-
Notifications
You must be signed in to change notification settings - Fork 0
/
search_fatlobyte.cpp
243 lines (191 loc) · 6.09 KB
/
search_fatlobyte.cpp
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
// search_std-strstr.cpp
/*
* proggen_searchwords
* Copyright (C) 2011 Alexander Korsunsky
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "search_fatlobyte.hpp"
#include <cstring>
#include <cassert>
#include <cstdint>
#include <iostream>
#include <fstream>
#include <numeric>
#include <algorithm>
#include <tuple>
#include <unordered_map>
#ifndef INDEX_TYPE
typedef std::uint32_t index_type;
#else
typedef INDEX_TYPE index_type;
#endif
#ifndef MAX_BLOCKSIZE
# define MAX_BLOCKSIZE 4
#endif
template <unsigned> struct blocktype {};
template <> struct blocktype<1> { typedef char type; };
template <> struct blocktype<2> { typedef std::int16_t type; };
template <> struct blocktype<4> { typedef std::int32_t type; };
template <> struct blocktype<8> { typedef std::int64_t type; };
template <unsigned Blocksize>
using indexmap_t = std::unordered_map<typename blocktype<Blocksize>::type, std::vector<index_type>> ;
typedef std::tuple<
indexmap_t<1>[1],
indexmap_t<2>[2],
indexmap_t<4>[4],
indexmap_t<8>[8]
> indexmap_tuple_type;
struct TextInfo
{
char const* id;
char const* text;
indexmap_tuple_type indexmaps;
index_type hit_count;
};
struct PatternInfo {
char const* pattern;
index_type length;
};
constexpr unsigned power_of_two(unsigned x) // sorta log_2()
{
return
(x) ? // if (x != 0) {
((x-1) ? // if (x-1 != 0) {
1+power_of_two(x >> 1)
: // } else {
0) // }
: // } else {
0
// }
;
}
template <unsigned Blocksize>
void create_indexmaps(indexmap_tuple_type& indexmap_tuple, const char* text)
{
constexpr unsigned indexmap_index = power_of_two(Blocksize);
index_type pos = 0;
while (*(text+pos+Blocksize))
{
std::get<indexmap_index>(indexmap_tuple)[pos % Blocksize][
*reinterpret_cast<const typename blocktype<Blocksize>::type*>(text+pos)
].push_back(pos);
++pos;
}
create_indexmaps<Blocksize/2>(indexmap_tuple, text);
}
template <>
void create_indexmaps<0>(indexmap_tuple_type&, const char*) {}
void SearchFatLobyte::addText( char const * id, char const * text )
{
std::unique_ptr<TextInfo> textinfo{new TextInfo{
id, text,
{},
0
}};
create_indexmaps<MAX_BLOCKSIZE>(textinfo->indexmaps, text);
_texts.emplace_back(std::move(textinfo));
}
void SearchFatLobyte::addPattern( char const * pattern )
{
_patterns.emplace_back(
new PatternInfo{pattern, static_cast<index_type>(strlen(pattern))}
);
}
void SearchFatLobyte::clearPatterns(void)
{ _patterns.clear(); }
inline bool expand_match(
const char* text, index_type match_index,
const char* pattern, index_type pattern_index,
index_type matchwidth
)
{
const char* text_p;
const char* pattern_p;
// Search forward
text_p = text + match_index + matchwidth;
pattern_p = pattern + pattern_index + matchwidth;
while(*pattern_p)
if(*text_p++ != *pattern_p++) return false;
#if 1
// search backwards
text_p = text + match_index - 1;
pattern_p = pattern + pattern_index - 1;
while(pattern_index)
{
if(*text_p-- != *pattern_p--) return false;
--pattern_index;
}
#endif
return true;
}
template <unsigned Blocksize>
void do_search(TextInfo& textinfo, const PatternInfo& patterninfo)
{
constexpr unsigned indexmap_index = power_of_two(Blocksize);
// If the Pattern is smaller than one block, we need to search with
// smaller blocks
if (patterninfo.length < Blocksize)
{
do_search<Blocksize/2>(textinfo, patterninfo);
return;
}
// Get a block that is most characteristic for the pattern
typename blocktype<Blocksize>::type char_block =
*reinterpret_cast<const typename blocktype<Blocksize>::type*>(
patterninfo.pattern
);
// iterate over all frames
for (index_type cur_frame = 0; cur_frame < Blocksize; ++cur_frame)
{
// find the first character in the index map
auto index_vec_it = std::get<indexmap_index>(textinfo.indexmaps)[cur_frame]
.find(char_block);
// if the currect character is unknown, we didn't find it
if(index_vec_it ==
std::get<indexmap_index>(textinfo.indexmaps)[cur_frame].end())
continue;
assert(!index_vec_it->second.empty()); // this shouldn't be possible
// iterate over ALL the indices available for the first character
for(index_type offset: index_vec_it->second)
{
if(expand_match(textinfo.text, offset, patterninfo.pattern, 0, Blocksize))
++textinfo.hit_count;
}
}
}
// specialize for leaf node
template <>
void do_search<0>(TextInfo& textinfo, const PatternInfo& patterninfo) {}
int SearchFatLobyte::seek( char const * filename )
{
std::ofstream found_file(filename);
if (!found_file)
{
perror("Failed to open output file");
std::cerr<<"Aborting search\n";
return 0;
}
for (auto& cur_text: _texts)
{
for (auto& cur_pat : _patterns)
do_search<MAX_BLOCKSIZE>(*cur_text, *cur_pat);
if (cur_text->hit_count)
found_file<<cur_text->id<<'\t'<<cur_text->hit_count<<'\n';
}
return std::accumulate(_texts.begin(), _texts.end(), index_type(0), // begin, end, init
[](index_type& acc, std::unique_ptr<TextInfo>& el) { return acc + el->hit_count; }
);
}
SearchFatLobyte::SearchFatLobyte() {}
SearchFatLobyte::~SearchFatLobyte() {}