Repository navigation
Expand file tree
/
Copy pathKeywordStatistics.cpp
More file actions
176 lines (156 loc) · 4.91 KB
/
Copy pathKeywordStatistics.cpp
File metadata and controls
176 lines (156 loc) · 4.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include "KeywordStatistics.h"
#include <QRegularExpression>
const QVector<QString> KeywordStatistics::keywords =
{
"auto", "break", "case", "char", "const", "continue", "default", "do", "double", "else",
"enum", "extern", "float", "for", "goto", "if", "int", "long", "register", "return",
"short", "signed", "sizeof", "static", "struct", "switch", "typedef", "union", "unsigned",
"void", "volatile", "while", "inline", "restrict", "_Bool", "_Complex", "_Imaginary",
"_Alignas", "_Alignof", "_Atomic", "_Static_assert"
};
HashTable::HashTable()
{
table.resize(41);
searchCount = 0;
}
HashTable::~HashTable() {}
QMap<QString, int> HashTable::getResults() const { return results; }
int HashTable::getSearchCount() const { return searchCount; }
qint64 HashTable::getElapsedTime() const { return timer.elapsed(); }
int HashTable::hash(const QString& key)
{
int firstLetter = key[0].unicode() - 'a';
int lastLetter = key[key.length() - 1].unicode() - 'a';
return (firstLetter * 100 + lastLetter) % 41;
}
void HashTable::insert(const QString& key)
{
int index = hash(key);
while (!table[index].key.isEmpty() && table[index].key != key)
{
index = (index + 1) % table.size();
searchCount++; // 增加查找次数
}
searchCount++; // 对于最终的查找操作也增加计数
if (table[index].key.isEmpty())
{
table[index].key = key;
table[index].count = 1; // 初始化计数为1
results[key] = 1; // 更新结果映射
}
else
{
table[index].count++; // 已存在的关键字计数增加
results[key]++; // 更新结果映射
}
}
void HashTable::statistic(const QString& filename)
{
timer.start(); // 计时开始
QFile file(filename);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;
QTextStream in(&file);
QRegularExpression wordRegex("\\b[A-Za-z_]+\\b");
while (!in.atEnd())
{
QString line = in.readLine();
QRegularExpressionMatchIterator it = wordRegex.globalMatch(line);
while (it.hasNext())
{
QRegularExpressionMatch match = it.next();
QString word = match.captured(0);
if (keywords.contains(word))
{
insert(word);
searchCount++;
}
}
}
// 输出统计结果
//for (const auto& node : table)
// {
// if (!node.key.isEmpty())
// { // 非空槽位
// qDebug() << node.key << ":" << node.count;
// }
//}
//qDebug() << "Total search count:" << searchCount;
//qDebug() << "Elapsed time:" << timer.elapsed() << "milliseconds";
}
BinarySearch::BinarySearch()
{
sortedKeywords = keywords;
std::sort(sortedKeywords.begin(), sortedKeywords.end());
searchCount = 0;
}
BinarySearch::~BinarySearch() {}
QMap<QString, int> BinarySearch::getResults() const { return keywordCounts; }
int BinarySearch::getSearchCount() const { return searchCount; }
qint64 BinarySearch::getElapsedTime() const { return timer.elapsed(); }
void BinarySearch::statistic(const QString& filename)
{
timer.start(); // 计时开始
QFile file(filename);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;
QTextStream in(&file);
QRegularExpression wordRegex("\\b[A-Za-z_]+\\b");
// 初始化关键字计数器
for (const auto& keyword : keywords)
{
keywordCounts[keyword] = 0;
}
while (!in.atEnd())
{
QString line = in.readLine();
QRegularExpressionMatchIterator it = wordRegex.globalMatch(line);
while (it.hasNext())
{
QRegularExpressionMatch match = it.next();
QString word = match.captured(0);
if (keywords.contains(word))
{
// 使用二分查找确定是否为关键字并计数
if (binarySearch(word) != -1)
{
keywordCounts[word]++;
}
}
}
}
// 输出统计结果
//for (const auto& keyword : keywords)
// {
// if (keywordCounts[keyword] > 0)
// {
// qDebug() << keyword << ":" << keywordCounts[keyword];
// }
//}
//qDebug() << "Total search count:" << searchCount;
//qDebug() << "Elapsed time:" << timer.elapsed() << "milliseconds";
}
// BinarySearch类中的binarySearch方法
int BinarySearch::binarySearch(const QString& key)
{
int low = 0;
int high = sortedKeywords.size() - 1;
while (low <= high)
{
int mid = (low + high) / 2;
searchCount++; // 每次比较都增加查找次数
if (sortedKeywords[mid] < key)
{
low = mid + 1;
}
else if (sortedKeywords[mid] > key)
{
high = mid - 1;
}
else
{
return mid; // 找到关键字
}
}
return -1; // 未找到关键字
}