-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringMatch.cpp
More file actions
170 lines (138 loc) · 3.67 KB
/
StringMatch.cpp
File metadata and controls
170 lines (138 loc) · 3.67 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
// StringMatch.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
// #include <cstddef>
#include <cassert>
#include <memory>
#include <string>
#include <windows.h>
#include <fstream>
#include <sstream>
#include <codecvt>
#include "StringMatch_BOM.h"
#include "StringMatch_DNDM.h"
#include "StringMatch_Horspool.h"
std::wstring ReadFromFile(const wchar_t* filePath)
{
//------------------------------------------------------------------------------
const std::codecvt_mode le_bom =
static_cast<std::codecvt_mode>(std::little_endian |
std::generate_header |
std::consume_header);
typedef std::codecvt_utf16<wchar_t, 0x10ffff, le_bom> wcvt_utf16le_bom;
wchar_t buffer[] = L"\u0111\u0107\u010D";
//------------------------------------------------------------------------------
wcvt_utf16le_bom cvt(1);
std::wifstream ifs;
std::locale wloc(ifs.getloc(), &cvt);
ifs.imbue(wloc);
ifs.open(filePath, std::ios::in | std::ios::binary);
if (ifs)
{
std::wstring line;
std::wostringstream strstm;
while (std::getline(ifs, line))
{
strstm << line;
}
return strstm.str();
}
return L"";
}
int _tmain(int argc, _TCHAR* argv[])
{
LARGE_INTEGER counter, counter2, freq;
QueryPerformanceFrequency(&freq);
QueryPerformanceCounter(&counter);
QueryPerformanceCounter(&counter2);
std::wstring p = ReadFromFile(L"_pattern.txt");
std::wstring t = ReadFromFile(L"_text.txt");
typedef wchar_t Char_T;
std::vector<size_t> matches_STD;
std::vector<size_t> matches_DNDM;
StringMatch_DNDM<Char_T> _DNDM;
_DNDM.Compile(p.c_str(), p.length());
std::vector<int> matches_BOM;
StringMatch_BOM<Char_T> _BOM;
_BOM.Compile(p.c_str(), p.length());
std::vector<size_t> matchs_Horspool;
StringMatch_Horspool<Char_T> _horspool;
_horspool.Compile(p.c_str(), p.length());
for (int n = 0; n < 20; ++n)
{
QueryPerformanceCounter(&counter);
matches_DNDM = _DNDM.FindAll(t.c_str(), t.length());
QueryPerformanceCounter(&counter2);
double t1 = (counter2.QuadPart-counter.QuadPart)/(double)(freq.QuadPart);
matches_STD.clear();
QueryPerformanceCounter(&counter);
for (size_t off = 0; off < t.length(); /* */)
{
size_t pos;
if ((pos = t.find(p, off)) != (size_t)-1)
{
matches_STD.push_back(pos);
off = pos + p.length();
}
else
{
break;
}
}
QueryPerformanceCounter(&counter2);
double t2 = (counter2.QuadPart-counter.QuadPart)/(double)(freq.QuadPart);
QueryPerformanceCounter(&counter);
matches_BOM = _BOM.FindAll(t.c_str(), t.length());
QueryPerformanceCounter(&counter2);
double t3 = (counter2.QuadPart-counter.QuadPart)/(double)(freq.QuadPart);
QueryPerformanceCounter(&counter);
matchs_Horspool = _horspool.FindAll(t.c_str(), t.length());
QueryPerformanceCounter(&counter2);
double t4 = (counter2.QuadPart-counter.QuadPart)/(double)(freq.QuadPart);
printf("DNDM: %fs, std::string.find: %fs, BOM: %fs, Horspool: %fs \n", t1, t2, t3, t4);
}
if (matches_DNDM.size() == matches_STD.size())
{
for (size_t i = 0; i < matches_STD.size(); ++i)
{
if (matches_DNDM[i] != matches_STD[i])
{
printf("DNDM match failed!\n");
}
}
}
else
{
printf("DNDM match count failed!");
}
if (matches_BOM.size() == matches_STD.size())
{
for (size_t i = 0; i < matches_STD.size(); ++i)
{
if (matches_BOM[i] != matches_STD[i])
{
printf("BOM match failed!\n");
}
}
}
else
{
printf("BOM match count failed!");
}
if (matchs_Horspool.size() == matches_STD.size())
{
for (size_t i = 0; i < matches_STD.size(); ++i)
{
if (matchs_Horspool[i] != matches_STD[i])
{
printf("Horspool match failed!\n");
}
}
}
else
{
printf("Horspool match count failed!");
}
system("pause");
return 0;
}