dartsclone 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +20 -0
- data/.rspec +3 -0
- data/.travis.yml +11 -0
- data/CHANGELOG.md +2 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +8 -0
- data/LICENSE.txt +23 -0
- data/README.md +64 -0
- data/Rakefile +15 -0
- data/dartsclone.gemspec +27 -0
- data/ext/dartsclone/dartscloneext.cpp +8 -0
- data/ext/dartsclone/dartscloneext.hpp +229 -0
- data/ext/dartsclone/extconf.rb +8 -0
- data/ext/dartsclone/src/COPYING.md +11 -0
- data/ext/dartsclone/src/darts.h +1931 -0
- data/lib/dartsclone.rb +4 -0
- data/lib/dartsclone/version.rb +6 -0
- metadata +64 -0
@@ -0,0 +1,11 @@
|
|
1
|
+
# The BSD 2-clause license
|
2
|
+
|
3
|
+
Copyright (c) 2008-2014, Susumu Yata
|
4
|
+
All rights reserved.
|
5
|
+
|
6
|
+
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
7
|
+
|
8
|
+
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
9
|
+
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
10
|
+
|
11
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
@@ -0,0 +1,1931 @@
|
|
1
|
+
#ifndef DARTS_H_
|
2
|
+
#define DARTS_H_
|
3
|
+
|
4
|
+
#include <cstdio>
|
5
|
+
#include <exception>
|
6
|
+
#include <new>
|
7
|
+
|
8
|
+
#define DARTS_VERSION "0.32"
|
9
|
+
|
10
|
+
// DARTS_THROW() throws a <Darts::Exception> whose message starts with the
|
11
|
+
// file name and the line number. For example, DARTS_THROW("error message") at
|
12
|
+
// line 123 of "darts.h" throws a <Darts::Exception> which has a pointer to
|
13
|
+
// "darts.h:123: exception: error message". The message is available by using
|
14
|
+
// what() as well as that of <std::exception>.
|
15
|
+
#define DARTS_INT_TO_STR(value) #value
|
16
|
+
#define DARTS_LINE_TO_STR(line) DARTS_INT_TO_STR(line)
|
17
|
+
#define DARTS_LINE_STR DARTS_LINE_TO_STR(__LINE__)
|
18
|
+
#define DARTS_THROW(msg) throw Darts::Details::Exception( \
|
19
|
+
__FILE__ ":" DARTS_LINE_STR ": exception: " msg)
|
20
|
+
|
21
|
+
namespace Darts {
|
22
|
+
|
23
|
+
// The following namespace hides the internal types and classes.
|
24
|
+
namespace Details {
|
25
|
+
|
26
|
+
// This header assumes that <int> and <unsigned int> are 32-bit integer types.
|
27
|
+
//
|
28
|
+
// Darts-clone keeps values associated with keys. The type of the values is
|
29
|
+
// <value_type>. Note that the values must be positive integers because the
|
30
|
+
// most significant bit (MSB) of each value is used to represent whether the
|
31
|
+
// corresponding unit is a leaf or not. Also, the keys are represented by
|
32
|
+
// sequences of <char_type>s. <uchar_type> is the unsigned type of <char_type>.
|
33
|
+
typedef char char_type;
|
34
|
+
typedef unsigned char uchar_type;
|
35
|
+
typedef int value_type;
|
36
|
+
|
37
|
+
// The main structure of Darts-clone is an array of <DoubleArrayUnit>s, and the
|
38
|
+
// unit type is actually a wrapper of <id_type>.
|
39
|
+
typedef unsigned int id_type;
|
40
|
+
|
41
|
+
// <progress_func_type> is the type of callback functions for reporting the
|
42
|
+
// progress of building a dictionary. See also build() of <DoubleArray>.
|
43
|
+
// The 1st argument receives the progress value and the 2nd argument receives
|
44
|
+
// the maximum progress value. A usage example is to show the progress
|
45
|
+
// percentage, 100.0 * (the 1st argument) / (the 2nd argument).
|
46
|
+
typedef int (*progress_func_type)(std::size_t, std::size_t);
|
47
|
+
|
48
|
+
// <DoubleArrayUnit> is the type of double-array units and it is a wrapper of
|
49
|
+
// <id_type> in practice.
|
50
|
+
class DoubleArrayUnit {
|
51
|
+
public:
|
52
|
+
DoubleArrayUnit() : unit_() {}
|
53
|
+
|
54
|
+
// has_leaf() returns whether a leaf unit is immediately derived from the
|
55
|
+
// unit (true) or not (false).
|
56
|
+
bool has_leaf() const {
|
57
|
+
return ((unit_ >> 8) & 1) == 1;
|
58
|
+
}
|
59
|
+
// value() returns the value stored in the unit, and thus value() is
|
60
|
+
// available when and only when the unit is a leaf unit.
|
61
|
+
value_type value() const {
|
62
|
+
return static_cast<value_type>(unit_ & ((1U << 31) - 1));
|
63
|
+
}
|
64
|
+
|
65
|
+
// label() returns the label associted with the unit. Note that a leaf unit
|
66
|
+
// always returns an invalid label. For this feature, leaf unit's label()
|
67
|
+
// returns an <id_type> that has the MSB of 1.
|
68
|
+
id_type label() const {
|
69
|
+
return unit_ & ((1U << 31) | 0xFF);
|
70
|
+
}
|
71
|
+
// offset() returns the offset from the unit to its derived units.
|
72
|
+
id_type offset() const {
|
73
|
+
return (unit_ >> 10) << ((unit_ & (1U << 9)) >> 6);
|
74
|
+
}
|
75
|
+
|
76
|
+
private:
|
77
|
+
id_type unit_;
|
78
|
+
|
79
|
+
// Copyable.
|
80
|
+
};
|
81
|
+
|
82
|
+
// Darts-clone throws an <Exception> for memory allocation failure, invalid
|
83
|
+
// arguments or a too large offset. The last case means that there are too many
|
84
|
+
// keys in the given set of keys. Note that the `msg' of <Exception> must be a
|
85
|
+
// constant or static string because an <Exception> keeps only a pointer to
|
86
|
+
// that string.
|
87
|
+
class Exception : public std::exception {
|
88
|
+
public:
|
89
|
+
explicit Exception(const char *msg = NULL) throw() : msg_(msg) {}
|
90
|
+
Exception(const Exception &rhs) throw() : msg_(rhs.msg_) {}
|
91
|
+
virtual ~Exception() throw() {}
|
92
|
+
|
93
|
+
// <Exception> overrides what() of <std::exception>.
|
94
|
+
virtual const char *what() const throw() {
|
95
|
+
return (msg_ != NULL) ? msg_ : "";
|
96
|
+
}
|
97
|
+
|
98
|
+
private:
|
99
|
+
const char *msg_;
|
100
|
+
|
101
|
+
// Disallows operator=.
|
102
|
+
Exception &operator=(const Exception &);
|
103
|
+
};
|
104
|
+
|
105
|
+
} // namespace Details
|
106
|
+
|
107
|
+
// <DoubleArrayImpl> is the interface of Darts-clone. Note that other
|
108
|
+
// classes should not be accessed from outside.
|
109
|
+
//
|
110
|
+
// <DoubleArrayImpl> has 4 template arguments but only the 3rd one is used as
|
111
|
+
// the type of values. Note that the given <T> is used only from outside, and
|
112
|
+
// the internal value type is not changed from <Darts::Details::value_type>.
|
113
|
+
// In build(), given values are casted from <T> to <Darts::Details::value_type>
|
114
|
+
// by using static_cast. On the other hand, values are casted from
|
115
|
+
// <Darts::Details::value_type> to <T> in searching dictionaries.
|
116
|
+
template <typename, typename, typename T, typename>
|
117
|
+
class DoubleArrayImpl {
|
118
|
+
public:
|
119
|
+
// Even if this <value_type> is changed, the internal value type is still
|
120
|
+
// <Darts::Details::value_type>. Other types, such as 64-bit integer types
|
121
|
+
// and floating-point number types, should not be used.
|
122
|
+
typedef T value_type;
|
123
|
+
// A key is reprenseted by a sequence of <key_type>s. For example,
|
124
|
+
// exactMatchSearch() takes a <const key_type *>.
|
125
|
+
typedef Details::char_type key_type;
|
126
|
+
// In searching dictionaries, the values associated with the matched keys are
|
127
|
+
// stored into or returned as <result_type>s.
|
128
|
+
typedef value_type result_type;
|
129
|
+
|
130
|
+
// <result_pair_type> enables applications to get the lengths of the matched
|
131
|
+
// keys in addition to the values.
|
132
|
+
struct result_pair_type {
|
133
|
+
value_type value;
|
134
|
+
std::size_t length;
|
135
|
+
};
|
136
|
+
|
137
|
+
// The constructor initializes member variables with 0 and NULLs.
|
138
|
+
DoubleArrayImpl() : size_(0), array_(NULL), buf_(NULL) {}
|
139
|
+
// The destructor frees memory allocated for units and then initializes
|
140
|
+
// member variables with 0 and NULLs.
|
141
|
+
virtual ~DoubleArrayImpl() {
|
142
|
+
clear();
|
143
|
+
}
|
144
|
+
|
145
|
+
// <DoubleArrayImpl> has 2 kinds of set_result()s. The 1st set_result() is to
|
146
|
+
// set a value to a <value_type>. The 2nd set_result() is to set a value and
|
147
|
+
// a length to a <result_pair_type>. By using set_result()s, search methods
|
148
|
+
// can return the 2 kinds of results in the same way.
|
149
|
+
// Why the set_result()s are non-static? It is for compatibility.
|
150
|
+
//
|
151
|
+
// The 1st set_result() takes a length as the 3rd argument but it is not
|
152
|
+
// used. If a compiler does a good job, codes for getting the length may be
|
153
|
+
// removed.
|
154
|
+
void set_result(value_type *result, value_type value, std::size_t) const {
|
155
|
+
*result = value;
|
156
|
+
}
|
157
|
+
// The 2nd set_result() uses both `value' and `length'.
|
158
|
+
void set_result(result_pair_type *result,
|
159
|
+
value_type value, std::size_t length) const {
|
160
|
+
result->value = value;
|
161
|
+
result->length = length;
|
162
|
+
}
|
163
|
+
|
164
|
+
// set_array() calls clear() in order to free memory allocated to the old
|
165
|
+
// array and then sets a new array. This function is useful to set a memory-
|
166
|
+
// mapped array. Note that the array set by set_array() is not freed in
|
167
|
+
// clear() and the destructor of <DoubleArrayImpl>.
|
168
|
+
// set_array() can also set the size of the new array but the size is not
|
169
|
+
// used in search methods. So it works well even if the 2nd argument is 0 or
|
170
|
+
// omitted. Remember that size() and total_size() returns 0 in such a case.
|
171
|
+
void set_array(const void *ptr, std::size_t size = 0) {
|
172
|
+
clear();
|
173
|
+
array_ = static_cast<const unit_type *>(ptr);
|
174
|
+
size_ = size;
|
175
|
+
}
|
176
|
+
// array() returns a pointer to the array of units.
|
177
|
+
const void *array() const {
|
178
|
+
return array_;
|
179
|
+
}
|
180
|
+
|
181
|
+
// clear() frees memory allocated to units and then initializes member
|
182
|
+
// variables with 0 and NULLs. Note that clear() does not free memory if the
|
183
|
+
// array of units was set by set_array(). In such a case, `array_' is not
|
184
|
+
// NULL and `buf_' is NULL.
|
185
|
+
void clear() {
|
186
|
+
size_ = 0;
|
187
|
+
array_ = NULL;
|
188
|
+
if (buf_ != NULL) {
|
189
|
+
delete[] buf_;
|
190
|
+
buf_ = NULL;
|
191
|
+
}
|
192
|
+
}
|
193
|
+
|
194
|
+
// unit_size() returns the size of each unit. The size must be 4 bytes.
|
195
|
+
std::size_t unit_size() const {
|
196
|
+
return sizeof(unit_type);
|
197
|
+
}
|
198
|
+
// size() returns the number of units. It can be 0 if set_array() is used.
|
199
|
+
std::size_t size() const {
|
200
|
+
return size_;
|
201
|
+
}
|
202
|
+
// total_size() returns the number of bytes allocated to the array of units.
|
203
|
+
// It can be 0 if set_array() is used.
|
204
|
+
std::size_t total_size() const {
|
205
|
+
return unit_size() * size();
|
206
|
+
}
|
207
|
+
// nonzero_size() exists for compatibility. It always returns the number of
|
208
|
+
// units because it takes long time to count the number of non-zero units.
|
209
|
+
std::size_t nonzero_size() const {
|
210
|
+
return size();
|
211
|
+
}
|
212
|
+
|
213
|
+
// build() constructs a dictionary from given key-value pairs. If `lengths'
|
214
|
+
// is NULL, `keys' is handled as an array of zero-terminated strings. If
|
215
|
+
// `values' is NULL, the index in `keys' is associated with each key, i.e.
|
216
|
+
// the ith key has (i - 1) as its value.
|
217
|
+
// Note that the key-value pairs must be arranged in key order and the values
|
218
|
+
// must not be negative. Also, if there are duplicate keys, only the first
|
219
|
+
// pair will be stored in the resultant dictionary.
|
220
|
+
// `progress_func' is a pointer to a callback function. If it is not NULL,
|
221
|
+
// it will be called in build() so that the caller can check the progress of
|
222
|
+
// dictionary construction. For details, please see the definition of
|
223
|
+
// <Darts::Details::progress_func_type>.
|
224
|
+
// The return value of build() is 0, and it indicates the success of the
|
225
|
+
// operation. Otherwise, build() throws a <Darts::Exception>, which is a
|
226
|
+
// derived class of <std::exception>.
|
227
|
+
// build() uses another construction algorithm if `values' is not NULL. In
|
228
|
+
// this case, Darts-clone uses a Directed Acyclic Word Graph (DAWG) instead
|
229
|
+
// of a trie because a DAWG is likely to be more compact than a trie.
|
230
|
+
int build(std::size_t num_keys, const key_type * const *keys,
|
231
|
+
const std::size_t *lengths = NULL, const value_type *values = NULL,
|
232
|
+
Details::progress_func_type progress_func = NULL);
|
233
|
+
|
234
|
+
// open() reads an array of units from the specified file. And if it goes
|
235
|
+
// well, the old array will be freed and replaced with the new array read
|
236
|
+
// from the file. `offset' specifies the number of bytes to be skipped before
|
237
|
+
// reading an array. `size' specifies the number of bytes to be read from the
|
238
|
+
// file. If the `size' is 0, the whole file will be read.
|
239
|
+
// open() returns 0 iff the operation succeeds. Otherwise, it returns a
|
240
|
+
// non-zero value or throws a <Darts::Exception>. The exception is thrown
|
241
|
+
// when and only when a memory allocation fails.
|
242
|
+
int open(const char *file_name, const char *mode = "rb",
|
243
|
+
std::size_t offset = 0, std::size_t size = 0);
|
244
|
+
// save() writes the array of units into the specified file. `offset'
|
245
|
+
// specifies the number of bytes to be skipped before writing the array.
|
246
|
+
// open() returns 0 iff the operation succeeds. Otherwise, it returns a
|
247
|
+
// non-zero value.
|
248
|
+
int save(const char *file_name, const char *mode = "wb",
|
249
|
+
std::size_t offset = 0) const;
|
250
|
+
|
251
|
+
// The 1st exactMatchSearch() tests whether the given key exists or not, and
|
252
|
+
// if it exists, its value and length are set to `result'. Otherwise, the
|
253
|
+
// value and the length of `result' are set to -1 and 0 respectively.
|
254
|
+
// Note that if `length' is 0, `key' is handled as a zero-terminated string.
|
255
|
+
// `node_pos' specifies the start position of matching. This argument enables
|
256
|
+
// the combination of exactMatchSearch() and traverse(). For example, if you
|
257
|
+
// want to test "xyzA", "xyzBC", and "xyzDE", you can use traverse() to get
|
258
|
+
// the node position corresponding to "xyz" and then you can use
|
259
|
+
// exactMatchSearch() to test "A", "BC", and "DE" from that position.
|
260
|
+
// Note that the length of `result' indicates the length from the `node_pos'.
|
261
|
+
// In the above example, the lengths are { 1, 2, 2 }, not { 4, 5, 5 }.
|
262
|
+
template <class U>
|
263
|
+
void exactMatchSearch(const key_type *key, U &result,
|
264
|
+
std::size_t length = 0, std::size_t node_pos = 0) const {
|
265
|
+
result = exactMatchSearch<U>(key, length, node_pos);
|
266
|
+
}
|
267
|
+
// The 2nd exactMatchSearch() returns a result instead of updating the 2nd
|
268
|
+
// argument. So, the following exactMatchSearch() has only 3 arguments.
|
269
|
+
template <class U>
|
270
|
+
inline U exactMatchSearch(const key_type *key, std::size_t length = 0,
|
271
|
+
std::size_t node_pos = 0) const;
|
272
|
+
|
273
|
+
// commonPrefixSearch() searches for keys which match a prefix of the given
|
274
|
+
// string. If `length' is 0, `key' is handled as a zero-terminated string.
|
275
|
+
// The values and the lengths of at most `max_num_results' matched keys are
|
276
|
+
// stored in `results'. commonPrefixSearch() returns the number of matched
|
277
|
+
// keys. Note that the return value can be larger than `max_num_results' if
|
278
|
+
// there are more than `max_num_results' matches. If you want to get all the
|
279
|
+
// results, allocate more spaces and call commonPrefixSearch() again.
|
280
|
+
// `node_pos' works as well as in exactMatchSearch().
|
281
|
+
template <class U>
|
282
|
+
inline std::size_t commonPrefixSearch(const key_type *key, U *results,
|
283
|
+
std::size_t max_num_results, std::size_t length = 0,
|
284
|
+
std::size_t node_pos = 0) const;
|
285
|
+
|
286
|
+
// In Darts-clone, a dictionary is a deterministic finite-state automaton
|
287
|
+
// (DFA) and traverse() tests transitions on the DFA. The initial state is
|
288
|
+
// `node_pos' and traverse() chooses transitions labeled key[key_pos],
|
289
|
+
// key[key_pos + 1], ... in order. If there is not a transition labeled
|
290
|
+
// key[key_pos + i], traverse() terminates the transitions at that state and
|
291
|
+
// returns -2. Otherwise, traverse() ends without a termination and returns
|
292
|
+
// -1 or a nonnegative value, -1 indicates that the final state was not an
|
293
|
+
// accept state. When a nonnegative value is returned, it is the value
|
294
|
+
// associated with the final accept state. That is, traverse() returns the
|
295
|
+
// value associated with the given key if it exists. Note that traverse()
|
296
|
+
// updates `node_pos' and `key_pos' after each transition.
|
297
|
+
inline value_type traverse(const key_type *key, std::size_t &node_pos,
|
298
|
+
std::size_t &key_pos, std::size_t length = 0) const;
|
299
|
+
|
300
|
+
private:
|
301
|
+
typedef Details::uchar_type uchar_type;
|
302
|
+
typedef Details::id_type id_type;
|
303
|
+
typedef Details::DoubleArrayUnit unit_type;
|
304
|
+
|
305
|
+
std::size_t size_;
|
306
|
+
const unit_type *array_;
|
307
|
+
unit_type *buf_;
|
308
|
+
|
309
|
+
// Disallows copy and assignment.
|
310
|
+
DoubleArrayImpl(const DoubleArrayImpl &);
|
311
|
+
DoubleArrayImpl &operator=(const DoubleArrayImpl &);
|
312
|
+
};
|
313
|
+
|
314
|
+
// <DoubleArray> is the typical instance of <DoubleArrayImpl>. It uses <int>
|
315
|
+
// as the type of values and it is suitable for most cases.
|
316
|
+
typedef DoubleArrayImpl<void, void, int, void> DoubleArray;
|
317
|
+
|
318
|
+
// The interface section ends here. For using Darts-clone, there is no need
|
319
|
+
// to read the remaining section, which gives the implementation of
|
320
|
+
// Darts-clone.
|
321
|
+
|
322
|
+
//
|
323
|
+
// Member functions of DoubleArrayImpl (except build()).
|
324
|
+
//
|
325
|
+
|
326
|
+
template <typename A, typename B, typename T, typename C>
|
327
|
+
int DoubleArrayImpl<A, B, T, C>::open(const char *file_name,
|
328
|
+
const char *mode, std::size_t offset, std::size_t size) {
|
329
|
+
#ifdef _MSC_VER
|
330
|
+
std::FILE *file;
|
331
|
+
if (::fopen_s(&file, file_name, mode) != 0) {
|
332
|
+
return -1;
|
333
|
+
}
|
334
|
+
#else
|
335
|
+
std::FILE *file = std::fopen(file_name, mode);
|
336
|
+
if (file == NULL) {
|
337
|
+
return -1;
|
338
|
+
}
|
339
|
+
#endif
|
340
|
+
|
341
|
+
if (size == 0) {
|
342
|
+
if (std::fseek(file, 0, SEEK_END) != 0) {
|
343
|
+
std::fclose(file);
|
344
|
+
return -1;
|
345
|
+
}
|
346
|
+
size = std::ftell(file) - offset;
|
347
|
+
}
|
348
|
+
|
349
|
+
size /= unit_size();
|
350
|
+
if (size < 256 || (size & 0xFF) != 0) {
|
351
|
+
std::fclose(file);
|
352
|
+
return -1;
|
353
|
+
}
|
354
|
+
|
355
|
+
if (std::fseek(file, offset, SEEK_SET) != 0) {
|
356
|
+
std::fclose(file);
|
357
|
+
return -1;
|
358
|
+
}
|
359
|
+
|
360
|
+
unit_type units[256];
|
361
|
+
if (std::fread(units, unit_size(), 256, file) != 256) {
|
362
|
+
std::fclose(file);
|
363
|
+
return -1;
|
364
|
+
}
|
365
|
+
|
366
|
+
if (units[0].label() != '\0' || units[0].has_leaf() ||
|
367
|
+
units[0].offset() == 0 || units[0].offset() >= 512) {
|
368
|
+
std::fclose(file);
|
369
|
+
return -1;
|
370
|
+
}
|
371
|
+
for (id_type i = 1; i < 256; ++i) {
|
372
|
+
if (units[i].label() <= 0xFF && units[i].offset() >= size) {
|
373
|
+
std::fclose(file);
|
374
|
+
return -1;
|
375
|
+
}
|
376
|
+
}
|
377
|
+
|
378
|
+
unit_type *buf;
|
379
|
+
try {
|
380
|
+
buf = new unit_type[size];
|
381
|
+
for (id_type i = 0; i < 256; ++i) {
|
382
|
+
buf[i] = units[i];
|
383
|
+
}
|
384
|
+
} catch (const std::bad_alloc &) {
|
385
|
+
std::fclose(file);
|
386
|
+
DARTS_THROW("failed to open double-array: std::bad_alloc");
|
387
|
+
}
|
388
|
+
|
389
|
+
if (size > 256) {
|
390
|
+
if (std::fread(buf + 256, unit_size(), size - 256, file) != size - 256) {
|
391
|
+
std::fclose(file);
|
392
|
+
delete[] buf;
|
393
|
+
return -1;
|
394
|
+
}
|
395
|
+
}
|
396
|
+
std::fclose(file);
|
397
|
+
|
398
|
+
clear();
|
399
|
+
|
400
|
+
size_ = size;
|
401
|
+
array_ = buf;
|
402
|
+
buf_ = buf;
|
403
|
+
return 0;
|
404
|
+
}
|
405
|
+
|
406
|
+
template <typename A, typename B, typename T, typename C>
|
407
|
+
int DoubleArrayImpl<A, B, T, C>::save(const char *file_name,
|
408
|
+
const char *mode, std::size_t offset) const {
|
409
|
+
if (size() == 0) {
|
410
|
+
return -1;
|
411
|
+
}
|
412
|
+
|
413
|
+
#ifdef _MSC_VER
|
414
|
+
std::FILE *file;
|
415
|
+
if (::fopen_s(&file, file_name, mode) != 0) {
|
416
|
+
return -1;
|
417
|
+
}
|
418
|
+
#else
|
419
|
+
std::FILE *file = std::fopen(file_name, mode);
|
420
|
+
if (file == NULL) {
|
421
|
+
return -1;
|
422
|
+
}
|
423
|
+
#endif
|
424
|
+
|
425
|
+
if (std::fseek(file, offset, SEEK_SET) != 0) {
|
426
|
+
std::fclose(file);
|
427
|
+
return -1;
|
428
|
+
}
|
429
|
+
|
430
|
+
if (std::fwrite(array_, unit_size(), size(), file) != size()) {
|
431
|
+
std::fclose(file);
|
432
|
+
return -1;
|
433
|
+
}
|
434
|
+
std::fclose(file);
|
435
|
+
return 0;
|
436
|
+
}
|
437
|
+
|
438
|
+
template <typename A, typename B, typename T, typename C>
|
439
|
+
template <typename U>
|
440
|
+
inline U DoubleArrayImpl<A, B, T, C>::exactMatchSearch(const key_type *key,
|
441
|
+
std::size_t length, std::size_t node_pos) const {
|
442
|
+
U result;
|
443
|
+
set_result(&result, static_cast<value_type>(-1), 0);
|
444
|
+
|
445
|
+
unit_type unit = array_[node_pos];
|
446
|
+
if (length != 0) {
|
447
|
+
for (std::size_t i = 0; i < length; ++i) {
|
448
|
+
node_pos ^= unit.offset() ^ static_cast<uchar_type>(key[i]);
|
449
|
+
unit = array_[node_pos];
|
450
|
+
if (unit.label() != static_cast<uchar_type>(key[i])) {
|
451
|
+
return result;
|
452
|
+
}
|
453
|
+
}
|
454
|
+
} else {
|
455
|
+
for ( ; key[length] != '\0'; ++length) {
|
456
|
+
node_pos ^= unit.offset() ^ static_cast<uchar_type>(key[length]);
|
457
|
+
unit = array_[node_pos];
|
458
|
+
if (unit.label() != static_cast<uchar_type>(key[length])) {
|
459
|
+
return result;
|
460
|
+
}
|
461
|
+
}
|
462
|
+
}
|
463
|
+
|
464
|
+
if (!unit.has_leaf()) {
|
465
|
+
return result;
|
466
|
+
}
|
467
|
+
unit = array_[node_pos ^ unit.offset()];
|
468
|
+
set_result(&result, static_cast<value_type>(unit.value()), length);
|
469
|
+
return result;
|
470
|
+
}
|
471
|
+
|
472
|
+
template <typename A, typename B, typename T, typename C>
|
473
|
+
template <typename U>
|
474
|
+
inline std::size_t DoubleArrayImpl<A, B, T, C>::commonPrefixSearch(
|
475
|
+
const key_type *key, U *results, std::size_t max_num_results,
|
476
|
+
std::size_t length, std::size_t node_pos) const {
|
477
|
+
std::size_t num_results = 0;
|
478
|
+
|
479
|
+
unit_type unit = array_[node_pos];
|
480
|
+
node_pos ^= unit.offset();
|
481
|
+
if (length != 0) {
|
482
|
+
for (std::size_t i = 0; i < length; ++i) {
|
483
|
+
node_pos ^= static_cast<uchar_type>(key[i]);
|
484
|
+
unit = array_[node_pos];
|
485
|
+
if (unit.label() != static_cast<uchar_type>(key[i])) {
|
486
|
+
return num_results;
|
487
|
+
}
|
488
|
+
|
489
|
+
node_pos ^= unit.offset();
|
490
|
+
if (unit.has_leaf()) {
|
491
|
+
if (num_results < max_num_results) {
|
492
|
+
set_result(&results[num_results], static_cast<value_type>(
|
493
|
+
array_[node_pos].value()), i + 1);
|
494
|
+
}
|
495
|
+
++num_results;
|
496
|
+
}
|
497
|
+
}
|
498
|
+
} else {
|
499
|
+
for ( ; key[length] != '\0'; ++length) {
|
500
|
+
node_pos ^= static_cast<uchar_type>(key[length]);
|
501
|
+
unit = array_[node_pos];
|
502
|
+
if (unit.label() != static_cast<uchar_type>(key[length])) {
|
503
|
+
return num_results;
|
504
|
+
}
|
505
|
+
|
506
|
+
node_pos ^= unit.offset();
|
507
|
+
if (unit.has_leaf()) {
|
508
|
+
if (num_results < max_num_results) {
|
509
|
+
set_result(&results[num_results], static_cast<value_type>(
|
510
|
+
array_[node_pos].value()), length + 1);
|
511
|
+
}
|
512
|
+
++num_results;
|
513
|
+
}
|
514
|
+
}
|
515
|
+
}
|
516
|
+
|
517
|
+
return num_results;
|
518
|
+
}
|
519
|
+
|
520
|
+
template <typename A, typename B, typename T, typename C>
|
521
|
+
inline typename DoubleArrayImpl<A, B, T, C>::value_type
|
522
|
+
DoubleArrayImpl<A, B, T, C>::traverse(const key_type *key,
|
523
|
+
std::size_t &node_pos, std::size_t &key_pos, std::size_t length) const {
|
524
|
+
id_type id = static_cast<id_type>(node_pos);
|
525
|
+
unit_type unit = array_[id];
|
526
|
+
|
527
|
+
if (length != 0) {
|
528
|
+
for ( ; key_pos < length; ++key_pos) {
|
529
|
+
id ^= unit.offset() ^ static_cast<uchar_type>(key[key_pos]);
|
530
|
+
unit = array_[id];
|
531
|
+
if (unit.label() != static_cast<uchar_type>(key[key_pos])) {
|
532
|
+
return static_cast<value_type>(-2);
|
533
|
+
}
|
534
|
+
node_pos = id;
|
535
|
+
}
|
536
|
+
} else {
|
537
|
+
for ( ; key[key_pos] != '\0'; ++key_pos) {
|
538
|
+
id ^= unit.offset() ^ static_cast<uchar_type>(key[key_pos]);
|
539
|
+
unit = array_[id];
|
540
|
+
if (unit.label() != static_cast<uchar_type>(key[key_pos])) {
|
541
|
+
return static_cast<value_type>(-2);
|
542
|
+
}
|
543
|
+
node_pos = id;
|
544
|
+
}
|
545
|
+
}
|
546
|
+
|
547
|
+
if (!unit.has_leaf()) {
|
548
|
+
return static_cast<value_type>(-1);
|
549
|
+
}
|
550
|
+
unit = array_[id ^ unit.offset()];
|
551
|
+
return static_cast<value_type>(unit.value());
|
552
|
+
}
|
553
|
+
|
554
|
+
namespace Details {
|
555
|
+
|
556
|
+
//
|
557
|
+
// Memory management of array.
|
558
|
+
//
|
559
|
+
|
560
|
+
template <typename T>
|
561
|
+
class AutoArray {
|
562
|
+
public:
|
563
|
+
explicit AutoArray(T *array = NULL) : array_(array) {}
|
564
|
+
~AutoArray() {
|
565
|
+
clear();
|
566
|
+
}
|
567
|
+
|
568
|
+
const T &operator[](std::size_t id) const {
|
569
|
+
return array_[id];
|
570
|
+
}
|
571
|
+
T &operator[](std::size_t id) {
|
572
|
+
return array_[id];
|
573
|
+
}
|
574
|
+
|
575
|
+
bool empty() const {
|
576
|
+
return array_ == NULL;
|
577
|
+
}
|
578
|
+
|
579
|
+
void clear() {
|
580
|
+
if (array_ != NULL) {
|
581
|
+
delete[] array_;
|
582
|
+
array_ = NULL;
|
583
|
+
}
|
584
|
+
}
|
585
|
+
void swap(AutoArray *array) {
|
586
|
+
T *temp = array_;
|
587
|
+
array_ = array->array_;
|
588
|
+
array->array_ = temp;
|
589
|
+
}
|
590
|
+
void reset(T *array = NULL) {
|
591
|
+
AutoArray(array).swap(this);
|
592
|
+
}
|
593
|
+
|
594
|
+
private:
|
595
|
+
T *array_;
|
596
|
+
|
597
|
+
// Disallows copy and assignment.
|
598
|
+
AutoArray(const AutoArray &);
|
599
|
+
AutoArray &operator=(const AutoArray &);
|
600
|
+
};
|
601
|
+
|
602
|
+
//
|
603
|
+
// Memory management of resizable array.
|
604
|
+
//
|
605
|
+
|
606
|
+
template <typename T>
|
607
|
+
class AutoPool {
|
608
|
+
public:
|
609
|
+
AutoPool() : buf_(), size_(0), capacity_(0) {}
|
610
|
+
~AutoPool() { clear(); }
|
611
|
+
|
612
|
+
const T &operator[](std::size_t id) const {
|
613
|
+
return *(reinterpret_cast<const T *>(&buf_[0]) + id);
|
614
|
+
}
|
615
|
+
T &operator[](std::size_t id) {
|
616
|
+
return *(reinterpret_cast<T *>(&buf_[0]) + id);
|
617
|
+
}
|
618
|
+
|
619
|
+
bool empty() const {
|
620
|
+
return size_ == 0;
|
621
|
+
}
|
622
|
+
std::size_t size() const {
|
623
|
+
return size_;
|
624
|
+
}
|
625
|
+
|
626
|
+
void clear() {
|
627
|
+
resize(0);
|
628
|
+
buf_.clear();
|
629
|
+
size_ = 0;
|
630
|
+
capacity_ = 0;
|
631
|
+
}
|
632
|
+
|
633
|
+
void push_back(const T &value) {
|
634
|
+
append(value);
|
635
|
+
}
|
636
|
+
void pop_back() {
|
637
|
+
(*this)[--size_].~T();
|
638
|
+
}
|
639
|
+
|
640
|
+
void append() {
|
641
|
+
if (size_ == capacity_)
|
642
|
+
resize_buf(size_ + 1);
|
643
|
+
new(&(*this)[size_++]) T;
|
644
|
+
}
|
645
|
+
void append(const T &value) {
|
646
|
+
if (size_ == capacity_)
|
647
|
+
resize_buf(size_ + 1);
|
648
|
+
new(&(*this)[size_++]) T(value);
|
649
|
+
}
|
650
|
+
|
651
|
+
void resize(std::size_t size) {
|
652
|
+
while (size_ > size) {
|
653
|
+
(*this)[--size_].~T();
|
654
|
+
}
|
655
|
+
if (size > capacity_) {
|
656
|
+
resize_buf(size);
|
657
|
+
}
|
658
|
+
while (size_ < size) {
|
659
|
+
new(&(*this)[size_++]) T;
|
660
|
+
}
|
661
|
+
}
|
662
|
+
void resize(std::size_t size, const T &value) {
|
663
|
+
while (size_ > size) {
|
664
|
+
(*this)[--size_].~T();
|
665
|
+
}
|
666
|
+
if (size > capacity_) {
|
667
|
+
resize_buf(size);
|
668
|
+
}
|
669
|
+
while (size_ < size) {
|
670
|
+
new(&(*this)[size_++]) T(value);
|
671
|
+
}
|
672
|
+
}
|
673
|
+
|
674
|
+
void reserve(std::size_t size) {
|
675
|
+
if (size > capacity_) {
|
676
|
+
resize_buf(size);
|
677
|
+
}
|
678
|
+
}
|
679
|
+
|
680
|
+
private:
|
681
|
+
AutoArray<char> buf_;
|
682
|
+
std::size_t size_;
|
683
|
+
std::size_t capacity_;
|
684
|
+
|
685
|
+
// Disallows copy and assignment.
|
686
|
+
AutoPool(const AutoPool &);
|
687
|
+
AutoPool &operator=(const AutoPool &);
|
688
|
+
|
689
|
+
void resize_buf(std::size_t size);
|
690
|
+
};
|
691
|
+
|
692
|
+
template <typename T>
|
693
|
+
void AutoPool<T>::resize_buf(std::size_t size) {
|
694
|
+
std::size_t capacity;
|
695
|
+
if (size >= capacity_ * 2) {
|
696
|
+
capacity = size;
|
697
|
+
} else {
|
698
|
+
capacity = 1;
|
699
|
+
while (capacity < size) {
|
700
|
+
capacity <<= 1;
|
701
|
+
}
|
702
|
+
}
|
703
|
+
|
704
|
+
AutoArray<char> buf;
|
705
|
+
try {
|
706
|
+
buf.reset(new char[sizeof(T) * capacity]);
|
707
|
+
} catch (const std::bad_alloc &) {
|
708
|
+
DARTS_THROW("failed to resize pool: std::bad_alloc");
|
709
|
+
}
|
710
|
+
|
711
|
+
if (size_ > 0) {
|
712
|
+
T *src = reinterpret_cast<T *>(&buf_[0]);
|
713
|
+
T *dest = reinterpret_cast<T *>(&buf[0]);
|
714
|
+
for (std::size_t i = 0; i < size_; ++i) {
|
715
|
+
new(&dest[i]) T(src[i]);
|
716
|
+
src[i].~T();
|
717
|
+
}
|
718
|
+
}
|
719
|
+
|
720
|
+
buf_.swap(&buf);
|
721
|
+
capacity_ = capacity;
|
722
|
+
}
|
723
|
+
|
724
|
+
//
|
725
|
+
// Memory management of stack.
|
726
|
+
//
|
727
|
+
|
728
|
+
template <typename T>
|
729
|
+
class AutoStack {
|
730
|
+
public:
|
731
|
+
AutoStack() : pool_() {}
|
732
|
+
~AutoStack() {
|
733
|
+
clear();
|
734
|
+
}
|
735
|
+
|
736
|
+
const T &top() const {
|
737
|
+
return pool_[size() - 1];
|
738
|
+
}
|
739
|
+
T &top() {
|
740
|
+
return pool_[size() - 1];
|
741
|
+
}
|
742
|
+
|
743
|
+
bool empty() const {
|
744
|
+
return pool_.empty();
|
745
|
+
}
|
746
|
+
std::size_t size() const {
|
747
|
+
return pool_.size();
|
748
|
+
}
|
749
|
+
|
750
|
+
void push(const T &value) {
|
751
|
+
pool_.push_back(value);
|
752
|
+
}
|
753
|
+
void pop() {
|
754
|
+
pool_.pop_back();
|
755
|
+
}
|
756
|
+
|
757
|
+
void clear() {
|
758
|
+
pool_.clear();
|
759
|
+
}
|
760
|
+
|
761
|
+
private:
|
762
|
+
AutoPool<T> pool_;
|
763
|
+
|
764
|
+
// Disallows copy and assignment.
|
765
|
+
AutoStack(const AutoStack &);
|
766
|
+
AutoStack &operator=(const AutoStack &);
|
767
|
+
};
|
768
|
+
|
769
|
+
//
|
770
|
+
// Succinct bit vector.
|
771
|
+
//
|
772
|
+
|
773
|
+
class BitVector {
|
774
|
+
public:
|
775
|
+
BitVector() : units_(), ranks_(), num_ones_(0), size_(0) {}
|
776
|
+
~BitVector() {
|
777
|
+
clear();
|
778
|
+
}
|
779
|
+
|
780
|
+
bool operator[](std::size_t id) const {
|
781
|
+
return (units_[id / UNIT_SIZE] >> (id % UNIT_SIZE) & 1) == 1;
|
782
|
+
}
|
783
|
+
|
784
|
+
id_type rank(std::size_t id) const {
|
785
|
+
std::size_t unit_id = id / UNIT_SIZE;
|
786
|
+
return ranks_[unit_id] + pop_count(units_[unit_id]
|
787
|
+
& (~0U >> (UNIT_SIZE - (id % UNIT_SIZE) - 1)));
|
788
|
+
}
|
789
|
+
|
790
|
+
void set(std::size_t id, bool bit) {
|
791
|
+
if (bit) {
|
792
|
+
units_[id / UNIT_SIZE] |= 1U << (id % UNIT_SIZE);
|
793
|
+
} else {
|
794
|
+
units_[id / UNIT_SIZE] &= ~(1U << (id % UNIT_SIZE));
|
795
|
+
}
|
796
|
+
}
|
797
|
+
|
798
|
+
bool empty() const {
|
799
|
+
return units_.empty();
|
800
|
+
}
|
801
|
+
std::size_t num_ones() const {
|
802
|
+
return num_ones_;
|
803
|
+
}
|
804
|
+
std::size_t size() const {
|
805
|
+
return size_;
|
806
|
+
}
|
807
|
+
|
808
|
+
void append() {
|
809
|
+
if ((size_ % UNIT_SIZE) == 0) {
|
810
|
+
units_.append(0);
|
811
|
+
}
|
812
|
+
++size_;
|
813
|
+
}
|
814
|
+
void build();
|
815
|
+
|
816
|
+
void clear() {
|
817
|
+
units_.clear();
|
818
|
+
ranks_.clear();
|
819
|
+
}
|
820
|
+
|
821
|
+
private:
|
822
|
+
enum { UNIT_SIZE = sizeof(id_type) * 8 };
|
823
|
+
|
824
|
+
AutoPool<id_type> units_;
|
825
|
+
AutoArray<id_type> ranks_;
|
826
|
+
std::size_t num_ones_;
|
827
|
+
std::size_t size_;
|
828
|
+
|
829
|
+
// Disallows copy and assignment.
|
830
|
+
BitVector(const BitVector &);
|
831
|
+
BitVector &operator=(const BitVector &);
|
832
|
+
|
833
|
+
static id_type pop_count(id_type unit) {
|
834
|
+
unit = ((unit & 0xAAAAAAAA) >> 1) + (unit & 0x55555555);
|
835
|
+
unit = ((unit & 0xCCCCCCCC) >> 2) + (unit & 0x33333333);
|
836
|
+
unit = ((unit >> 4) + unit) & 0x0F0F0F0F;
|
837
|
+
unit += unit >> 8;
|
838
|
+
unit += unit >> 16;
|
839
|
+
return unit & 0xFF;
|
840
|
+
}
|
841
|
+
};
|
842
|
+
|
843
|
+
inline void BitVector::build() {
|
844
|
+
try {
|
845
|
+
ranks_.reset(new id_type[units_.size()]);
|
846
|
+
} catch (const std::bad_alloc &) {
|
847
|
+
DARTS_THROW("failed to build rank index: std::bad_alloc");
|
848
|
+
}
|
849
|
+
|
850
|
+
num_ones_ = 0;
|
851
|
+
for (std::size_t i = 0; i < units_.size(); ++i) {
|
852
|
+
ranks_[i] = num_ones_;
|
853
|
+
num_ones_ += pop_count(units_[i]);
|
854
|
+
}
|
855
|
+
}
|
856
|
+
|
857
|
+
//
|
858
|
+
// Keyset.
|
859
|
+
//
|
860
|
+
|
861
|
+
template <typename T>
|
862
|
+
class Keyset {
|
863
|
+
public:
|
864
|
+
Keyset(std::size_t num_keys, const char_type * const *keys,
|
865
|
+
const std::size_t *lengths, const T *values) :
|
866
|
+
num_keys_(num_keys), keys_(keys), lengths_(lengths), values_(values) {}
|
867
|
+
|
868
|
+
std::size_t num_keys() const {
|
869
|
+
return num_keys_;
|
870
|
+
}
|
871
|
+
const char_type *keys(std::size_t id) const {
|
872
|
+
return keys_[id];
|
873
|
+
}
|
874
|
+
uchar_type keys(std::size_t key_id, std::size_t char_id) const {
|
875
|
+
if (has_lengths() && char_id >= lengths_[key_id])
|
876
|
+
return '\0';
|
877
|
+
return keys_[key_id][char_id];
|
878
|
+
}
|
879
|
+
|
880
|
+
bool has_lengths() const {
|
881
|
+
return lengths_ != NULL;
|
882
|
+
}
|
883
|
+
std::size_t lengths(std::size_t id) const {
|
884
|
+
if (has_lengths()) {
|
885
|
+
return lengths_[id];
|
886
|
+
}
|
887
|
+
std::size_t length = 0;
|
888
|
+
while (keys_[id][length] != '\0') {
|
889
|
+
++length;
|
890
|
+
}
|
891
|
+
return length;
|
892
|
+
}
|
893
|
+
|
894
|
+
bool has_values() const {
|
895
|
+
return values_ != NULL;
|
896
|
+
}
|
897
|
+
const value_type values(std::size_t id) const {
|
898
|
+
if (has_values()) {
|
899
|
+
return static_cast<value_type>(values_[id]);
|
900
|
+
}
|
901
|
+
return static_cast<value_type>(id);
|
902
|
+
}
|
903
|
+
|
904
|
+
private:
|
905
|
+
std::size_t num_keys_;
|
906
|
+
const char_type * const * keys_;
|
907
|
+
const std::size_t *lengths_;
|
908
|
+
const T *values_;
|
909
|
+
|
910
|
+
// Disallows copy and assignment.
|
911
|
+
Keyset(const Keyset &);
|
912
|
+
Keyset &operator=(const Keyset &);
|
913
|
+
};
|
914
|
+
|
915
|
+
//
|
916
|
+
// Node of Directed Acyclic Word Graph (DAWG).
|
917
|
+
//
|
918
|
+
|
919
|
+
class DawgNode {
|
920
|
+
public:
|
921
|
+
DawgNode() : child_(0), sibling_(0), label_('\0'),
|
922
|
+
is_state_(false), has_sibling_(false) {}
|
923
|
+
|
924
|
+
void set_child(id_type child) {
|
925
|
+
child_ = child;
|
926
|
+
}
|
927
|
+
void set_sibling(id_type sibling) {
|
928
|
+
sibling_ = sibling;
|
929
|
+
}
|
930
|
+
void set_value(value_type value) {
|
931
|
+
child_ = value;
|
932
|
+
}
|
933
|
+
void set_label(uchar_type label) {
|
934
|
+
label_ = label;
|
935
|
+
}
|
936
|
+
void set_is_state(bool is_state) {
|
937
|
+
is_state_ = is_state;
|
938
|
+
}
|
939
|
+
void set_has_sibling(bool has_sibling) {
|
940
|
+
has_sibling_ = has_sibling;
|
941
|
+
}
|
942
|
+
|
943
|
+
id_type child() const {
|
944
|
+
return child_;
|
945
|
+
}
|
946
|
+
id_type sibling() const {
|
947
|
+
return sibling_;
|
948
|
+
}
|
949
|
+
value_type value() const {
|
950
|
+
return static_cast<value_type>(child_);
|
951
|
+
}
|
952
|
+
uchar_type label() const {
|
953
|
+
return label_;
|
954
|
+
}
|
955
|
+
bool is_state() const {
|
956
|
+
return is_state_;
|
957
|
+
}
|
958
|
+
bool has_sibling() const {
|
959
|
+
return has_sibling_;
|
960
|
+
}
|
961
|
+
|
962
|
+
id_type unit() const {
|
963
|
+
if (label_ == '\0') {
|
964
|
+
return (child_ << 1) | (has_sibling_ ? 1 : 0);
|
965
|
+
}
|
966
|
+
return (child_ << 2) | (is_state_ ? 2 : 0) | (has_sibling_ ? 1 : 0);
|
967
|
+
}
|
968
|
+
|
969
|
+
private:
|
970
|
+
id_type child_;
|
971
|
+
id_type sibling_;
|
972
|
+
uchar_type label_;
|
973
|
+
bool is_state_;
|
974
|
+
bool has_sibling_;
|
975
|
+
|
976
|
+
// Copyable.
|
977
|
+
};
|
978
|
+
|
979
|
+
//
|
980
|
+
// Fixed unit of Directed Acyclic Word Graph (DAWG).
|
981
|
+
//
|
982
|
+
|
983
|
+
class DawgUnit {
|
984
|
+
public:
|
985
|
+
explicit DawgUnit(id_type unit = 0) : unit_(unit) {}
|
986
|
+
DawgUnit(const DawgUnit &unit) : unit_(unit.unit_) {}
|
987
|
+
|
988
|
+
DawgUnit &operator=(id_type unit) {
|
989
|
+
unit_ = unit;
|
990
|
+
return *this;
|
991
|
+
}
|
992
|
+
|
993
|
+
id_type unit() const {
|
994
|
+
return unit_;
|
995
|
+
}
|
996
|
+
|
997
|
+
id_type child() const {
|
998
|
+
return unit_ >> 2;
|
999
|
+
}
|
1000
|
+
bool has_sibling() const {
|
1001
|
+
return (unit_ & 1) == 1;
|
1002
|
+
}
|
1003
|
+
value_type value() const {
|
1004
|
+
return static_cast<value_type>(unit_ >> 1);
|
1005
|
+
}
|
1006
|
+
bool is_state() const {
|
1007
|
+
return (unit_ & 2) == 2;
|
1008
|
+
}
|
1009
|
+
|
1010
|
+
private:
|
1011
|
+
id_type unit_;
|
1012
|
+
|
1013
|
+
// Copyable.
|
1014
|
+
};
|
1015
|
+
|
1016
|
+
//
|
1017
|
+
// Directed Acyclic Word Graph (DAWG) builder.
|
1018
|
+
//
|
1019
|
+
|
1020
|
+
class DawgBuilder {
|
1021
|
+
public:
|
1022
|
+
DawgBuilder() : nodes_(), units_(), labels_(), is_intersections_(),
|
1023
|
+
table_(), node_stack_(), recycle_bin_(), num_states_(0) {}
|
1024
|
+
~DawgBuilder() {
|
1025
|
+
clear();
|
1026
|
+
}
|
1027
|
+
|
1028
|
+
id_type root() const {
|
1029
|
+
return 0;
|
1030
|
+
}
|
1031
|
+
|
1032
|
+
id_type child(id_type id) const {
|
1033
|
+
return units_[id].child();
|
1034
|
+
}
|
1035
|
+
id_type sibling(id_type id) const {
|
1036
|
+
return units_[id].has_sibling() ? (id + 1) : 0;
|
1037
|
+
}
|
1038
|
+
int value(id_type id) const {
|
1039
|
+
return units_[id].value();
|
1040
|
+
}
|
1041
|
+
|
1042
|
+
bool is_leaf(id_type id) const {
|
1043
|
+
return label(id) == '\0';
|
1044
|
+
}
|
1045
|
+
uchar_type label(id_type id) const {
|
1046
|
+
return labels_[id];
|
1047
|
+
}
|
1048
|
+
|
1049
|
+
bool is_intersection(id_type id) const {
|
1050
|
+
return is_intersections_[id];
|
1051
|
+
}
|
1052
|
+
id_type intersection_id(id_type id) const {
|
1053
|
+
return is_intersections_.rank(id) - 1;
|
1054
|
+
}
|
1055
|
+
|
1056
|
+
std::size_t num_intersections() const {
|
1057
|
+
return is_intersections_.num_ones();
|
1058
|
+
}
|
1059
|
+
|
1060
|
+
std::size_t size() const {
|
1061
|
+
return units_.size();
|
1062
|
+
}
|
1063
|
+
|
1064
|
+
void init();
|
1065
|
+
void finish();
|
1066
|
+
|
1067
|
+
void insert(const char *key, std::size_t length, value_type value);
|
1068
|
+
|
1069
|
+
void clear();
|
1070
|
+
|
1071
|
+
private:
|
1072
|
+
enum { INITIAL_TABLE_SIZE = 1 << 10 };
|
1073
|
+
|
1074
|
+
AutoPool<DawgNode> nodes_;
|
1075
|
+
AutoPool<DawgUnit> units_;
|
1076
|
+
AutoPool<uchar_type> labels_;
|
1077
|
+
BitVector is_intersections_;
|
1078
|
+
AutoPool<id_type> table_;
|
1079
|
+
AutoStack<id_type> node_stack_;
|
1080
|
+
AutoStack<id_type> recycle_bin_;
|
1081
|
+
std::size_t num_states_;
|
1082
|
+
|
1083
|
+
// Disallows copy and assignment.
|
1084
|
+
DawgBuilder(const DawgBuilder &);
|
1085
|
+
DawgBuilder &operator=(const DawgBuilder &);
|
1086
|
+
|
1087
|
+
void flush(id_type id);
|
1088
|
+
|
1089
|
+
void expand_table();
|
1090
|
+
|
1091
|
+
id_type find_unit(id_type id, id_type *hash_id) const;
|
1092
|
+
id_type find_node(id_type node_id, id_type *hash_id) const;
|
1093
|
+
|
1094
|
+
bool are_equal(id_type node_id, id_type unit_id) const;
|
1095
|
+
|
1096
|
+
id_type hash_unit(id_type id) const;
|
1097
|
+
id_type hash_node(id_type id) const;
|
1098
|
+
|
1099
|
+
id_type append_node();
|
1100
|
+
id_type append_unit();
|
1101
|
+
|
1102
|
+
void free_node(id_type id) {
|
1103
|
+
recycle_bin_.push(id);
|
1104
|
+
}
|
1105
|
+
|
1106
|
+
static id_type hash(id_type key) {
|
1107
|
+
key = ~key + (key << 15); // key = (key << 15) - key - 1;
|
1108
|
+
key = key ^ (key >> 12);
|
1109
|
+
key = key + (key << 2);
|
1110
|
+
key = key ^ (key >> 4);
|
1111
|
+
key = key * 2057; // key = (key + (key << 3)) + (key << 11);
|
1112
|
+
key = key ^ (key >> 16);
|
1113
|
+
return key;
|
1114
|
+
}
|
1115
|
+
};
|
1116
|
+
|
1117
|
+
inline void DawgBuilder::init() {
|
1118
|
+
table_.resize(INITIAL_TABLE_SIZE, 0);
|
1119
|
+
|
1120
|
+
append_node();
|
1121
|
+
append_unit();
|
1122
|
+
|
1123
|
+
num_states_ = 1;
|
1124
|
+
|
1125
|
+
nodes_[0].set_label(0xFF);
|
1126
|
+
node_stack_.push(0);
|
1127
|
+
}
|
1128
|
+
|
1129
|
+
inline void DawgBuilder::finish() {
|
1130
|
+
flush(0);
|
1131
|
+
|
1132
|
+
units_[0] = nodes_[0].unit();
|
1133
|
+
labels_[0] = nodes_[0].label();
|
1134
|
+
|
1135
|
+
nodes_.clear();
|
1136
|
+
table_.clear();
|
1137
|
+
node_stack_.clear();
|
1138
|
+
recycle_bin_.clear();
|
1139
|
+
|
1140
|
+
is_intersections_.build();
|
1141
|
+
}
|
1142
|
+
|
1143
|
+
inline void DawgBuilder::insert(const char *key, std::size_t length,
|
1144
|
+
value_type value) {
|
1145
|
+
if (value < 0) {
|
1146
|
+
DARTS_THROW("failed to insert key: negative value");
|
1147
|
+
} else if (length == 0) {
|
1148
|
+
DARTS_THROW("failed to insert key: zero-length key");
|
1149
|
+
}
|
1150
|
+
|
1151
|
+
id_type id = 0;
|
1152
|
+
std::size_t key_pos = 0;
|
1153
|
+
|
1154
|
+
for ( ; key_pos <= length; ++key_pos) {
|
1155
|
+
id_type child_id = nodes_[id].child();
|
1156
|
+
if (child_id == 0) {
|
1157
|
+
break;
|
1158
|
+
}
|
1159
|
+
|
1160
|
+
uchar_type key_label = static_cast<uchar_type>(key[key_pos]);
|
1161
|
+
if (key_pos < length && key_label == '\0') {
|
1162
|
+
DARTS_THROW("failed to insert key: invalid null character");
|
1163
|
+
}
|
1164
|
+
|
1165
|
+
uchar_type unit_label = nodes_[child_id].label();
|
1166
|
+
if (key_label < unit_label) {
|
1167
|
+
DARTS_THROW("failed to insert key: wrong key order");
|
1168
|
+
} else if (key_label > unit_label) {
|
1169
|
+
nodes_[child_id].set_has_sibling(true);
|
1170
|
+
flush(child_id);
|
1171
|
+
break;
|
1172
|
+
}
|
1173
|
+
id = child_id;
|
1174
|
+
}
|
1175
|
+
|
1176
|
+
if (key_pos > length) {
|
1177
|
+
return;
|
1178
|
+
}
|
1179
|
+
|
1180
|
+
for ( ; key_pos <= length; ++key_pos) {
|
1181
|
+
uchar_type key_label = static_cast<uchar_type>(
|
1182
|
+
(key_pos < length) ? key[key_pos] : '\0');
|
1183
|
+
id_type child_id = append_node();
|
1184
|
+
|
1185
|
+
if (nodes_[id].child() == 0) {
|
1186
|
+
nodes_[child_id].set_is_state(true);
|
1187
|
+
}
|
1188
|
+
nodes_[child_id].set_sibling(nodes_[id].child());
|
1189
|
+
nodes_[child_id].set_label(key_label);
|
1190
|
+
nodes_[id].set_child(child_id);
|
1191
|
+
node_stack_.push(child_id);
|
1192
|
+
|
1193
|
+
id = child_id;
|
1194
|
+
}
|
1195
|
+
nodes_[id].set_value(value);
|
1196
|
+
}
|
1197
|
+
|
1198
|
+
inline void DawgBuilder::clear() {
|
1199
|
+
nodes_.clear();
|
1200
|
+
units_.clear();
|
1201
|
+
labels_.clear();
|
1202
|
+
is_intersections_.clear();
|
1203
|
+
table_.clear();
|
1204
|
+
node_stack_.clear();
|
1205
|
+
recycle_bin_.clear();
|
1206
|
+
num_states_ = 0;
|
1207
|
+
}
|
1208
|
+
|
1209
|
+
inline void DawgBuilder::flush(id_type id) {
|
1210
|
+
while (node_stack_.top() != id) {
|
1211
|
+
id_type node_id = node_stack_.top();
|
1212
|
+
node_stack_.pop();
|
1213
|
+
|
1214
|
+
if (num_states_ >= table_.size() - (table_.size() >> 2)) {
|
1215
|
+
expand_table();
|
1216
|
+
}
|
1217
|
+
|
1218
|
+
id_type num_siblings = 0;
|
1219
|
+
for (id_type i = node_id; i != 0; i = nodes_[i].sibling()) {
|
1220
|
+
++num_siblings;
|
1221
|
+
}
|
1222
|
+
|
1223
|
+
id_type hash_id;
|
1224
|
+
id_type match_id = find_node(node_id, &hash_id);
|
1225
|
+
if (match_id != 0) {
|
1226
|
+
is_intersections_.set(match_id, true);
|
1227
|
+
} else {
|
1228
|
+
id_type unit_id = 0;
|
1229
|
+
for (id_type i = 0; i < num_siblings; ++i) {
|
1230
|
+
unit_id = append_unit();
|
1231
|
+
}
|
1232
|
+
for (id_type i = node_id; i != 0; i = nodes_[i].sibling()) {
|
1233
|
+
units_[unit_id] = nodes_[i].unit();
|
1234
|
+
labels_[unit_id] = nodes_[i].label();
|
1235
|
+
--unit_id;
|
1236
|
+
}
|
1237
|
+
match_id = unit_id + 1;
|
1238
|
+
table_[hash_id] = match_id;
|
1239
|
+
++num_states_;
|
1240
|
+
}
|
1241
|
+
|
1242
|
+
for (id_type i = node_id, next; i != 0; i = next) {
|
1243
|
+
next = nodes_[i].sibling();
|
1244
|
+
free_node(i);
|
1245
|
+
}
|
1246
|
+
|
1247
|
+
nodes_[node_stack_.top()].set_child(match_id);
|
1248
|
+
}
|
1249
|
+
node_stack_.pop();
|
1250
|
+
}
|
1251
|
+
|
1252
|
+
inline void DawgBuilder::expand_table() {
|
1253
|
+
std::size_t table_size = table_.size() << 1;
|
1254
|
+
table_.clear();
|
1255
|
+
table_.resize(table_size, 0);
|
1256
|
+
|
1257
|
+
for (std::size_t i = 1; i < units_.size(); ++i) {
|
1258
|
+
id_type id = static_cast<id_type>(i);
|
1259
|
+
if (labels_[id] == '\0' || units_[id].is_state()) {
|
1260
|
+
id_type hash_id;
|
1261
|
+
find_unit(id, &hash_id);
|
1262
|
+
table_[hash_id] = id;
|
1263
|
+
}
|
1264
|
+
}
|
1265
|
+
}
|
1266
|
+
|
1267
|
+
inline id_type DawgBuilder::find_unit(id_type id, id_type *hash_id) const {
|
1268
|
+
*hash_id = hash_unit(id) % table_.size();
|
1269
|
+
for ( ; ; *hash_id = (*hash_id + 1) % table_.size()) {
|
1270
|
+
id_type unit_id = table_[*hash_id];
|
1271
|
+
if (unit_id == 0) {
|
1272
|
+
break;
|
1273
|
+
}
|
1274
|
+
|
1275
|
+
// There must not be the same unit.
|
1276
|
+
}
|
1277
|
+
return 0;
|
1278
|
+
}
|
1279
|
+
|
1280
|
+
inline id_type DawgBuilder::find_node(id_type node_id,
|
1281
|
+
id_type *hash_id) const {
|
1282
|
+
*hash_id = hash_node(node_id) % table_.size();
|
1283
|
+
for ( ; ; *hash_id = (*hash_id + 1) % table_.size()) {
|
1284
|
+
id_type unit_id = table_[*hash_id];
|
1285
|
+
if (unit_id == 0) {
|
1286
|
+
break;
|
1287
|
+
}
|
1288
|
+
|
1289
|
+
if (are_equal(node_id, unit_id)) {
|
1290
|
+
return unit_id;
|
1291
|
+
}
|
1292
|
+
}
|
1293
|
+
return 0;
|
1294
|
+
}
|
1295
|
+
|
1296
|
+
inline bool DawgBuilder::are_equal(id_type node_id, id_type unit_id) const {
|
1297
|
+
for (id_type i = nodes_[node_id].sibling(); i != 0;
|
1298
|
+
i = nodes_[i].sibling()) {
|
1299
|
+
if (units_[unit_id].has_sibling() == false) {
|
1300
|
+
return false;
|
1301
|
+
}
|
1302
|
+
++unit_id;
|
1303
|
+
}
|
1304
|
+
if (units_[unit_id].has_sibling() == true) {
|
1305
|
+
return false;
|
1306
|
+
}
|
1307
|
+
|
1308
|
+
for (id_type i = node_id; i != 0; i = nodes_[i].sibling(), --unit_id) {
|
1309
|
+
if (nodes_[i].unit() != units_[unit_id].unit() ||
|
1310
|
+
nodes_[i].label() != labels_[unit_id]) {
|
1311
|
+
return false;
|
1312
|
+
}
|
1313
|
+
}
|
1314
|
+
return true;
|
1315
|
+
}
|
1316
|
+
|
1317
|
+
inline id_type DawgBuilder::hash_unit(id_type id) const {
|
1318
|
+
id_type hash_value = 0;
|
1319
|
+
for ( ; id != 0; ++id) {
|
1320
|
+
id_type unit = units_[id].unit();
|
1321
|
+
uchar_type label = labels_[id];
|
1322
|
+
hash_value ^= hash((label << 24) ^ unit);
|
1323
|
+
|
1324
|
+
if (units_[id].has_sibling() == false) {
|
1325
|
+
break;
|
1326
|
+
}
|
1327
|
+
}
|
1328
|
+
return hash_value;
|
1329
|
+
}
|
1330
|
+
|
1331
|
+
inline id_type DawgBuilder::hash_node(id_type id) const {
|
1332
|
+
id_type hash_value = 0;
|
1333
|
+
for ( ; id != 0; id = nodes_[id].sibling()) {
|
1334
|
+
id_type unit = nodes_[id].unit();
|
1335
|
+
uchar_type label = nodes_[id].label();
|
1336
|
+
hash_value ^= hash((label << 24) ^ unit);
|
1337
|
+
}
|
1338
|
+
return hash_value;
|
1339
|
+
}
|
1340
|
+
|
1341
|
+
inline id_type DawgBuilder::append_unit() {
|
1342
|
+
is_intersections_.append();
|
1343
|
+
units_.append();
|
1344
|
+
labels_.append();
|
1345
|
+
|
1346
|
+
return static_cast<id_type>(is_intersections_.size() - 1);
|
1347
|
+
}
|
1348
|
+
|
1349
|
+
inline id_type DawgBuilder::append_node() {
|
1350
|
+
id_type id;
|
1351
|
+
if (recycle_bin_.empty()) {
|
1352
|
+
id = static_cast<id_type>(nodes_.size());
|
1353
|
+
nodes_.append();
|
1354
|
+
} else {
|
1355
|
+
id = recycle_bin_.top();
|
1356
|
+
nodes_[id] = DawgNode();
|
1357
|
+
recycle_bin_.pop();
|
1358
|
+
}
|
1359
|
+
return id;
|
1360
|
+
}
|
1361
|
+
|
1362
|
+
//
|
1363
|
+
// Unit of double-array builder.
|
1364
|
+
//
|
1365
|
+
|
1366
|
+
class DoubleArrayBuilderUnit {
|
1367
|
+
public:
|
1368
|
+
DoubleArrayBuilderUnit() : unit_(0) {}
|
1369
|
+
|
1370
|
+
void set_has_leaf(bool has_leaf) {
|
1371
|
+
if (has_leaf) {
|
1372
|
+
unit_ |= 1U << 8;
|
1373
|
+
} else {
|
1374
|
+
unit_ &= ~(1U << 8);
|
1375
|
+
}
|
1376
|
+
}
|
1377
|
+
void set_value(value_type value) {
|
1378
|
+
unit_ = value | (1U << 31);
|
1379
|
+
}
|
1380
|
+
void set_label(uchar_type label) {
|
1381
|
+
unit_ = (unit_ & ~0xFFU) | label;
|
1382
|
+
}
|
1383
|
+
void set_offset(id_type offset) {
|
1384
|
+
if (offset >= 1U << 29) {
|
1385
|
+
DARTS_THROW("failed to modify unit: too large offset");
|
1386
|
+
}
|
1387
|
+
unit_ &= (1U << 31) | (1U << 8) | 0xFF;
|
1388
|
+
if (offset < 1U << 21) {
|
1389
|
+
unit_ |= (offset << 10);
|
1390
|
+
} else {
|
1391
|
+
unit_ |= (offset << 2) | (1U << 9);
|
1392
|
+
}
|
1393
|
+
}
|
1394
|
+
|
1395
|
+
private:
|
1396
|
+
id_type unit_;
|
1397
|
+
|
1398
|
+
// Copyable.
|
1399
|
+
};
|
1400
|
+
|
1401
|
+
//
|
1402
|
+
// Extra unit of double-array builder.
|
1403
|
+
//
|
1404
|
+
|
1405
|
+
class DoubleArrayBuilderExtraUnit {
|
1406
|
+
public:
|
1407
|
+
DoubleArrayBuilderExtraUnit() : prev_(0), next_(0),
|
1408
|
+
is_fixed_(false), is_used_(false) {}
|
1409
|
+
|
1410
|
+
void set_prev(id_type prev) {
|
1411
|
+
prev_ = prev;
|
1412
|
+
}
|
1413
|
+
void set_next(id_type next) {
|
1414
|
+
next_ = next;
|
1415
|
+
}
|
1416
|
+
void set_is_fixed(bool is_fixed) {
|
1417
|
+
is_fixed_ = is_fixed;
|
1418
|
+
}
|
1419
|
+
void set_is_used(bool is_used) {
|
1420
|
+
is_used_ = is_used;
|
1421
|
+
}
|
1422
|
+
|
1423
|
+
id_type prev() const {
|
1424
|
+
return prev_;
|
1425
|
+
}
|
1426
|
+
id_type next() const {
|
1427
|
+
return next_;
|
1428
|
+
}
|
1429
|
+
bool is_fixed() const {
|
1430
|
+
return is_fixed_;
|
1431
|
+
}
|
1432
|
+
bool is_used() const {
|
1433
|
+
return is_used_;
|
1434
|
+
}
|
1435
|
+
|
1436
|
+
private:
|
1437
|
+
id_type prev_;
|
1438
|
+
id_type next_;
|
1439
|
+
bool is_fixed_;
|
1440
|
+
bool is_used_;
|
1441
|
+
|
1442
|
+
// Copyable.
|
1443
|
+
};
|
1444
|
+
|
1445
|
+
//
|
1446
|
+
// DAWG -> double-array converter.
|
1447
|
+
//
|
1448
|
+
|
1449
|
+
class DoubleArrayBuilder {
|
1450
|
+
public:
|
1451
|
+
explicit DoubleArrayBuilder(progress_func_type progress_func)
|
1452
|
+
: progress_func_(progress_func), units_(), extras_(), labels_(),
|
1453
|
+
table_(), extras_head_(0) {}
|
1454
|
+
~DoubleArrayBuilder() {
|
1455
|
+
clear();
|
1456
|
+
}
|
1457
|
+
|
1458
|
+
template <typename T>
|
1459
|
+
void build(const Keyset<T> &keyset);
|
1460
|
+
void copy(std::size_t *size_ptr, DoubleArrayUnit **buf_ptr) const;
|
1461
|
+
|
1462
|
+
void clear();
|
1463
|
+
|
1464
|
+
private:
|
1465
|
+
enum { BLOCK_SIZE = 256 };
|
1466
|
+
enum { NUM_EXTRA_BLOCKS = 16 };
|
1467
|
+
enum { NUM_EXTRAS = BLOCK_SIZE * NUM_EXTRA_BLOCKS };
|
1468
|
+
|
1469
|
+
enum { UPPER_MASK = 0xFF << 21 };
|
1470
|
+
enum { LOWER_MASK = 0xFF };
|
1471
|
+
|
1472
|
+
typedef DoubleArrayBuilderUnit unit_type;
|
1473
|
+
typedef DoubleArrayBuilderExtraUnit extra_type;
|
1474
|
+
|
1475
|
+
progress_func_type progress_func_;
|
1476
|
+
AutoPool<unit_type> units_;
|
1477
|
+
AutoArray<extra_type> extras_;
|
1478
|
+
AutoPool<uchar_type> labels_;
|
1479
|
+
AutoArray<id_type> table_;
|
1480
|
+
id_type extras_head_;
|
1481
|
+
|
1482
|
+
// Disallows copy and assignment.
|
1483
|
+
DoubleArrayBuilder(const DoubleArrayBuilder &);
|
1484
|
+
DoubleArrayBuilder &operator=(const DoubleArrayBuilder &);
|
1485
|
+
|
1486
|
+
std::size_t num_blocks() const {
|
1487
|
+
return units_.size() / BLOCK_SIZE;
|
1488
|
+
}
|
1489
|
+
|
1490
|
+
const extra_type &extras(id_type id) const {
|
1491
|
+
return extras_[id % NUM_EXTRAS];
|
1492
|
+
}
|
1493
|
+
extra_type &extras(id_type id) {
|
1494
|
+
return extras_[id % NUM_EXTRAS];
|
1495
|
+
}
|
1496
|
+
|
1497
|
+
template <typename T>
|
1498
|
+
void build_dawg(const Keyset<T> &keyset, DawgBuilder *dawg_builder);
|
1499
|
+
void build_from_dawg(const DawgBuilder &dawg);
|
1500
|
+
void build_from_dawg(const DawgBuilder &dawg,
|
1501
|
+
id_type dawg_id, id_type dic_id);
|
1502
|
+
id_type arrange_from_dawg(const DawgBuilder &dawg,
|
1503
|
+
id_type dawg_id, id_type dic_id);
|
1504
|
+
|
1505
|
+
template <typename T>
|
1506
|
+
void build_from_keyset(const Keyset<T> &keyset);
|
1507
|
+
template <typename T>
|
1508
|
+
void build_from_keyset(const Keyset<T> &keyset, std::size_t begin,
|
1509
|
+
std::size_t end, std::size_t depth, id_type dic_id);
|
1510
|
+
template <typename T>
|
1511
|
+
id_type arrange_from_keyset(const Keyset<T> &keyset, std::size_t begin,
|
1512
|
+
std::size_t end, std::size_t depth, id_type dic_id);
|
1513
|
+
|
1514
|
+
id_type find_valid_offset(id_type id) const;
|
1515
|
+
bool is_valid_offset(id_type id, id_type offset) const;
|
1516
|
+
|
1517
|
+
void reserve_id(id_type id);
|
1518
|
+
void expand_units();
|
1519
|
+
|
1520
|
+
void fix_all_blocks();
|
1521
|
+
void fix_block(id_type block_id);
|
1522
|
+
};
|
1523
|
+
|
1524
|
+
template <typename T>
|
1525
|
+
void DoubleArrayBuilder::build(const Keyset<T> &keyset) {
|
1526
|
+
if (keyset.has_values()) {
|
1527
|
+
Details::DawgBuilder dawg_builder;
|
1528
|
+
build_dawg(keyset, &dawg_builder);
|
1529
|
+
build_from_dawg(dawg_builder);
|
1530
|
+
dawg_builder.clear();
|
1531
|
+
} else {
|
1532
|
+
build_from_keyset(keyset);
|
1533
|
+
}
|
1534
|
+
}
|
1535
|
+
|
1536
|
+
inline void DoubleArrayBuilder::copy(std::size_t *size_ptr,
|
1537
|
+
DoubleArrayUnit **buf_ptr) const {
|
1538
|
+
if (size_ptr != NULL) {
|
1539
|
+
*size_ptr = units_.size();
|
1540
|
+
}
|
1541
|
+
if (buf_ptr != NULL) {
|
1542
|
+
*buf_ptr = new DoubleArrayUnit[units_.size()];
|
1543
|
+
unit_type *units = reinterpret_cast<unit_type *>(*buf_ptr);
|
1544
|
+
for (std::size_t i = 0; i < units_.size(); ++i) {
|
1545
|
+
units[i] = units_[i];
|
1546
|
+
}
|
1547
|
+
}
|
1548
|
+
}
|
1549
|
+
|
1550
|
+
inline void DoubleArrayBuilder::clear() {
|
1551
|
+
units_.clear();
|
1552
|
+
extras_.clear();
|
1553
|
+
labels_.clear();
|
1554
|
+
table_.clear();
|
1555
|
+
extras_head_ = 0;
|
1556
|
+
}
|
1557
|
+
|
1558
|
+
template <typename T>
|
1559
|
+
void DoubleArrayBuilder::build_dawg(const Keyset<T> &keyset,
|
1560
|
+
DawgBuilder *dawg_builder) {
|
1561
|
+
dawg_builder->init();
|
1562
|
+
for (std::size_t i = 0; i < keyset.num_keys(); ++i) {
|
1563
|
+
dawg_builder->insert(keyset.keys(i), keyset.lengths(i), keyset.values(i));
|
1564
|
+
if (progress_func_ != NULL) {
|
1565
|
+
progress_func_(i + 1, keyset.num_keys() + 1);
|
1566
|
+
}
|
1567
|
+
}
|
1568
|
+
dawg_builder->finish();
|
1569
|
+
}
|
1570
|
+
|
1571
|
+
inline void DoubleArrayBuilder::build_from_dawg(const DawgBuilder &dawg) {
|
1572
|
+
std::size_t num_units = 1;
|
1573
|
+
while (num_units < dawg.size()) {
|
1574
|
+
num_units <<= 1;
|
1575
|
+
}
|
1576
|
+
units_.reserve(num_units);
|
1577
|
+
|
1578
|
+
table_.reset(new id_type[dawg.num_intersections()]);
|
1579
|
+
for (std::size_t i = 0; i < dawg.num_intersections(); ++i) {
|
1580
|
+
table_[i] = 0;
|
1581
|
+
}
|
1582
|
+
|
1583
|
+
extras_.reset(new extra_type[NUM_EXTRAS]);
|
1584
|
+
|
1585
|
+
reserve_id(0);
|
1586
|
+
extras(0).set_is_used(true);
|
1587
|
+
units_[0].set_offset(1);
|
1588
|
+
units_[0].set_label('\0');
|
1589
|
+
|
1590
|
+
if (dawg.child(dawg.root()) != 0) {
|
1591
|
+
build_from_dawg(dawg, dawg.root(), 0);
|
1592
|
+
}
|
1593
|
+
|
1594
|
+
fix_all_blocks();
|
1595
|
+
|
1596
|
+
extras_.clear();
|
1597
|
+
labels_.clear();
|
1598
|
+
table_.clear();
|
1599
|
+
}
|
1600
|
+
|
1601
|
+
inline void DoubleArrayBuilder::build_from_dawg(const DawgBuilder &dawg,
|
1602
|
+
id_type dawg_id, id_type dic_id) {
|
1603
|
+
id_type dawg_child_id = dawg.child(dawg_id);
|
1604
|
+
if (dawg.is_intersection(dawg_child_id)) {
|
1605
|
+
id_type intersection_id = dawg.intersection_id(dawg_child_id);
|
1606
|
+
id_type offset = table_[intersection_id];
|
1607
|
+
if (offset != 0) {
|
1608
|
+
offset ^= dic_id;
|
1609
|
+
if (!(offset & UPPER_MASK) || !(offset & LOWER_MASK)) {
|
1610
|
+
if (dawg.is_leaf(dawg_child_id)) {
|
1611
|
+
units_[dic_id].set_has_leaf(true);
|
1612
|
+
}
|
1613
|
+
units_[dic_id].set_offset(offset);
|
1614
|
+
return;
|
1615
|
+
}
|
1616
|
+
}
|
1617
|
+
}
|
1618
|
+
|
1619
|
+
id_type offset = arrange_from_dawg(dawg, dawg_id, dic_id);
|
1620
|
+
if (dawg.is_intersection(dawg_child_id)) {
|
1621
|
+
table_[dawg.intersection_id(dawg_child_id)] = offset;
|
1622
|
+
}
|
1623
|
+
|
1624
|
+
do {
|
1625
|
+
uchar_type child_label = dawg.label(dawg_child_id);
|
1626
|
+
id_type dic_child_id = offset ^ child_label;
|
1627
|
+
if (child_label != '\0') {
|
1628
|
+
build_from_dawg(dawg, dawg_child_id, dic_child_id);
|
1629
|
+
}
|
1630
|
+
dawg_child_id = dawg.sibling(dawg_child_id);
|
1631
|
+
} while (dawg_child_id != 0);
|
1632
|
+
}
|
1633
|
+
|
1634
|
+
inline id_type DoubleArrayBuilder::arrange_from_dawg(const DawgBuilder &dawg,
|
1635
|
+
id_type dawg_id, id_type dic_id) {
|
1636
|
+
labels_.resize(0);
|
1637
|
+
|
1638
|
+
id_type dawg_child_id = dawg.child(dawg_id);
|
1639
|
+
while (dawg_child_id != 0) {
|
1640
|
+
labels_.append(dawg.label(dawg_child_id));
|
1641
|
+
dawg_child_id = dawg.sibling(dawg_child_id);
|
1642
|
+
}
|
1643
|
+
|
1644
|
+
id_type offset = find_valid_offset(dic_id);
|
1645
|
+
units_[dic_id].set_offset(dic_id ^ offset);
|
1646
|
+
|
1647
|
+
dawg_child_id = dawg.child(dawg_id);
|
1648
|
+
for (std::size_t i = 0; i < labels_.size(); ++i) {
|
1649
|
+
id_type dic_child_id = offset ^ labels_[i];
|
1650
|
+
reserve_id(dic_child_id);
|
1651
|
+
|
1652
|
+
if (dawg.is_leaf(dawg_child_id)) {
|
1653
|
+
units_[dic_id].set_has_leaf(true);
|
1654
|
+
units_[dic_child_id].set_value(dawg.value(dawg_child_id));
|
1655
|
+
} else {
|
1656
|
+
units_[dic_child_id].set_label(labels_[i]);
|
1657
|
+
}
|
1658
|
+
|
1659
|
+
dawg_child_id = dawg.sibling(dawg_child_id);
|
1660
|
+
}
|
1661
|
+
extras(offset).set_is_used(true);
|
1662
|
+
|
1663
|
+
return offset;
|
1664
|
+
}
|
1665
|
+
|
1666
|
+
template <typename T>
|
1667
|
+
void DoubleArrayBuilder::build_from_keyset(const Keyset<T> &keyset) {
|
1668
|
+
std::size_t num_units = 1;
|
1669
|
+
while (num_units < keyset.num_keys()) {
|
1670
|
+
num_units <<= 1;
|
1671
|
+
}
|
1672
|
+
units_.reserve(num_units);
|
1673
|
+
|
1674
|
+
extras_.reset(new extra_type[NUM_EXTRAS]);
|
1675
|
+
|
1676
|
+
reserve_id(0);
|
1677
|
+
extras(0).set_is_used(true);
|
1678
|
+
units_[0].set_offset(1);
|
1679
|
+
units_[0].set_label('\0');
|
1680
|
+
|
1681
|
+
if (keyset.num_keys() > 0) {
|
1682
|
+
build_from_keyset(keyset, 0, keyset.num_keys(), 0, 0);
|
1683
|
+
}
|
1684
|
+
|
1685
|
+
fix_all_blocks();
|
1686
|
+
|
1687
|
+
extras_.clear();
|
1688
|
+
labels_.clear();
|
1689
|
+
}
|
1690
|
+
|
1691
|
+
template <typename T>
|
1692
|
+
void DoubleArrayBuilder::build_from_keyset(const Keyset<T> &keyset,
|
1693
|
+
std::size_t begin, std::size_t end, std::size_t depth, id_type dic_id) {
|
1694
|
+
id_type offset = arrange_from_keyset(keyset, begin, end, depth, dic_id);
|
1695
|
+
|
1696
|
+
while (begin < end) {
|
1697
|
+
if (keyset.keys(begin, depth) != '\0') {
|
1698
|
+
break;
|
1699
|
+
}
|
1700
|
+
++begin;
|
1701
|
+
}
|
1702
|
+
if (begin == end) {
|
1703
|
+
return;
|
1704
|
+
}
|
1705
|
+
|
1706
|
+
std::size_t last_begin = begin;
|
1707
|
+
uchar_type last_label = keyset.keys(begin, depth);
|
1708
|
+
while (++begin < end) {
|
1709
|
+
uchar_type label = keyset.keys(begin, depth);
|
1710
|
+
if (label != last_label) {
|
1711
|
+
build_from_keyset(keyset, last_begin, begin,
|
1712
|
+
depth + 1, offset ^ last_label);
|
1713
|
+
last_begin = begin;
|
1714
|
+
last_label = keyset.keys(begin, depth);
|
1715
|
+
}
|
1716
|
+
}
|
1717
|
+
build_from_keyset(keyset, last_begin, end, depth + 1, offset ^ last_label);
|
1718
|
+
}
|
1719
|
+
|
1720
|
+
template <typename T>
|
1721
|
+
id_type DoubleArrayBuilder::arrange_from_keyset(const Keyset<T> &keyset,
|
1722
|
+
std::size_t begin, std::size_t end, std::size_t depth, id_type dic_id) {
|
1723
|
+
labels_.resize(0);
|
1724
|
+
|
1725
|
+
value_type value = -1;
|
1726
|
+
for (std::size_t i = begin; i < end; ++i) {
|
1727
|
+
uchar_type label = keyset.keys(i, depth);
|
1728
|
+
if (label == '\0') {
|
1729
|
+
if (keyset.has_lengths() && depth < keyset.lengths(i)) {
|
1730
|
+
DARTS_THROW("failed to build double-array: "
|
1731
|
+
"invalid null character");
|
1732
|
+
} else if (keyset.values(i) < 0) {
|
1733
|
+
DARTS_THROW("failed to build double-array: negative value");
|
1734
|
+
}
|
1735
|
+
|
1736
|
+
if (value == -1) {
|
1737
|
+
value = keyset.values(i);
|
1738
|
+
}
|
1739
|
+
if (progress_func_ != NULL) {
|
1740
|
+
progress_func_(i + 1, keyset.num_keys() + 1);
|
1741
|
+
}
|
1742
|
+
}
|
1743
|
+
|
1744
|
+
if (labels_.empty()) {
|
1745
|
+
labels_.append(label);
|
1746
|
+
} else if (label != labels_[labels_.size() - 1]) {
|
1747
|
+
if (label < labels_[labels_.size() - 1]) {
|
1748
|
+
DARTS_THROW("failed to build double-array: wrong key order");
|
1749
|
+
}
|
1750
|
+
labels_.append(label);
|
1751
|
+
}
|
1752
|
+
}
|
1753
|
+
|
1754
|
+
id_type offset = find_valid_offset(dic_id);
|
1755
|
+
units_[dic_id].set_offset(dic_id ^ offset);
|
1756
|
+
|
1757
|
+
for (std::size_t i = 0; i < labels_.size(); ++i) {
|
1758
|
+
id_type dic_child_id = offset ^ labels_[i];
|
1759
|
+
reserve_id(dic_child_id);
|
1760
|
+
if (labels_[i] == '\0') {
|
1761
|
+
units_[dic_id].set_has_leaf(true);
|
1762
|
+
units_[dic_child_id].set_value(value);
|
1763
|
+
} else {
|
1764
|
+
units_[dic_child_id].set_label(labels_[i]);
|
1765
|
+
}
|
1766
|
+
}
|
1767
|
+
extras(offset).set_is_used(true);
|
1768
|
+
|
1769
|
+
return offset;
|
1770
|
+
}
|
1771
|
+
|
1772
|
+
inline id_type DoubleArrayBuilder::find_valid_offset(id_type id) const {
|
1773
|
+
if (extras_head_ >= units_.size()) {
|
1774
|
+
return units_.size() | (id & LOWER_MASK);
|
1775
|
+
}
|
1776
|
+
|
1777
|
+
id_type unfixed_id = extras_head_;
|
1778
|
+
do {
|
1779
|
+
id_type offset = unfixed_id ^ labels_[0];
|
1780
|
+
if (is_valid_offset(id, offset)) {
|
1781
|
+
return offset;
|
1782
|
+
}
|
1783
|
+
unfixed_id = extras(unfixed_id).next();
|
1784
|
+
} while (unfixed_id != extras_head_);
|
1785
|
+
|
1786
|
+
return units_.size() | (id & LOWER_MASK);
|
1787
|
+
}
|
1788
|
+
|
1789
|
+
inline bool DoubleArrayBuilder::is_valid_offset(id_type id,
|
1790
|
+
id_type offset) const {
|
1791
|
+
if (extras(offset).is_used()) {
|
1792
|
+
return false;
|
1793
|
+
}
|
1794
|
+
|
1795
|
+
id_type rel_offset = id ^ offset;
|
1796
|
+
if ((rel_offset & LOWER_MASK) && (rel_offset & UPPER_MASK)) {
|
1797
|
+
return false;
|
1798
|
+
}
|
1799
|
+
|
1800
|
+
for (std::size_t i = 1; i < labels_.size(); ++i) {
|
1801
|
+
if (extras(offset ^ labels_[i]).is_fixed()) {
|
1802
|
+
return false;
|
1803
|
+
}
|
1804
|
+
}
|
1805
|
+
|
1806
|
+
return true;
|
1807
|
+
}
|
1808
|
+
|
1809
|
+
inline void DoubleArrayBuilder::reserve_id(id_type id) {
|
1810
|
+
if (id >= units_.size()) {
|
1811
|
+
expand_units();
|
1812
|
+
}
|
1813
|
+
|
1814
|
+
if (id == extras_head_) {
|
1815
|
+
extras_head_ = extras(id).next();
|
1816
|
+
if (extras_head_ == id) {
|
1817
|
+
extras_head_ = units_.size();
|
1818
|
+
}
|
1819
|
+
}
|
1820
|
+
extras(extras(id).prev()).set_next(extras(id).next());
|
1821
|
+
extras(extras(id).next()).set_prev(extras(id).prev());
|
1822
|
+
extras(id).set_is_fixed(true);
|
1823
|
+
}
|
1824
|
+
|
1825
|
+
inline void DoubleArrayBuilder::expand_units() {
|
1826
|
+
id_type src_num_units = units_.size();
|
1827
|
+
id_type src_num_blocks = num_blocks();
|
1828
|
+
|
1829
|
+
id_type dest_num_units = src_num_units + BLOCK_SIZE;
|
1830
|
+
id_type dest_num_blocks = src_num_blocks + 1;
|
1831
|
+
|
1832
|
+
if (dest_num_blocks > NUM_EXTRA_BLOCKS) {
|
1833
|
+
fix_block(src_num_blocks - NUM_EXTRA_BLOCKS);
|
1834
|
+
}
|
1835
|
+
|
1836
|
+
units_.resize(dest_num_units);
|
1837
|
+
|
1838
|
+
if (dest_num_blocks > NUM_EXTRA_BLOCKS) {
|
1839
|
+
for (std::size_t id = src_num_units; id < dest_num_units; ++id) {
|
1840
|
+
extras(id).set_is_used(false);
|
1841
|
+
extras(id).set_is_fixed(false);
|
1842
|
+
}
|
1843
|
+
}
|
1844
|
+
|
1845
|
+
for (id_type i = src_num_units + 1; i < dest_num_units; ++i) {
|
1846
|
+
extras(i - 1).set_next(i);
|
1847
|
+
extras(i).set_prev(i - 1);
|
1848
|
+
}
|
1849
|
+
|
1850
|
+
extras(src_num_units).set_prev(dest_num_units - 1);
|
1851
|
+
extras(dest_num_units - 1).set_next(src_num_units);
|
1852
|
+
|
1853
|
+
extras(src_num_units).set_prev(extras(extras_head_).prev());
|
1854
|
+
extras(dest_num_units - 1).set_next(extras_head_);
|
1855
|
+
|
1856
|
+
extras(extras(extras_head_).prev()).set_next(src_num_units);
|
1857
|
+
extras(extras_head_).set_prev(dest_num_units - 1);
|
1858
|
+
}
|
1859
|
+
|
1860
|
+
inline void DoubleArrayBuilder::fix_all_blocks() {
|
1861
|
+
id_type begin = 0;
|
1862
|
+
if (num_blocks() > NUM_EXTRA_BLOCKS) {
|
1863
|
+
begin = num_blocks() - NUM_EXTRA_BLOCKS;
|
1864
|
+
}
|
1865
|
+
id_type end = num_blocks();
|
1866
|
+
|
1867
|
+
for (id_type block_id = begin; block_id != end; ++block_id) {
|
1868
|
+
fix_block(block_id);
|
1869
|
+
}
|
1870
|
+
}
|
1871
|
+
|
1872
|
+
inline void DoubleArrayBuilder::fix_block(id_type block_id) {
|
1873
|
+
id_type begin = block_id * BLOCK_SIZE;
|
1874
|
+
id_type end = begin + BLOCK_SIZE;
|
1875
|
+
|
1876
|
+
id_type unused_offset = 0;
|
1877
|
+
for (id_type offset = begin; offset != end; ++offset) {
|
1878
|
+
if (!extras(offset).is_used()) {
|
1879
|
+
unused_offset = offset;
|
1880
|
+
break;
|
1881
|
+
}
|
1882
|
+
}
|
1883
|
+
|
1884
|
+
for (id_type id = begin; id != end; ++id) {
|
1885
|
+
if (!extras(id).is_fixed()) {
|
1886
|
+
reserve_id(id);
|
1887
|
+
units_[id].set_label(static_cast<uchar_type>(id ^ unused_offset));
|
1888
|
+
}
|
1889
|
+
}
|
1890
|
+
}
|
1891
|
+
|
1892
|
+
} // namespace Details
|
1893
|
+
|
1894
|
+
//
|
1895
|
+
// Member function build() of DoubleArrayImpl.
|
1896
|
+
//
|
1897
|
+
|
1898
|
+
template <typename A, typename B, typename T, typename C>
|
1899
|
+
int DoubleArrayImpl<A, B, T, C>::build(std::size_t num_keys,
|
1900
|
+
const key_type * const *keys, const std::size_t *lengths,
|
1901
|
+
const value_type *values, Details::progress_func_type progress_func) {
|
1902
|
+
Details::Keyset<value_type> keyset(num_keys, keys, lengths, values);
|
1903
|
+
|
1904
|
+
Details::DoubleArrayBuilder builder(progress_func);
|
1905
|
+
builder.build(keyset);
|
1906
|
+
|
1907
|
+
std::size_t size = 0;
|
1908
|
+
unit_type *buf = NULL;
|
1909
|
+
builder.copy(&size, &buf);
|
1910
|
+
|
1911
|
+
clear();
|
1912
|
+
|
1913
|
+
size_ = size;
|
1914
|
+
array_ = buf;
|
1915
|
+
buf_ = buf;
|
1916
|
+
|
1917
|
+
if (progress_func != NULL) {
|
1918
|
+
progress_func(num_keys + 1, num_keys + 1);
|
1919
|
+
}
|
1920
|
+
|
1921
|
+
return 0;
|
1922
|
+
}
|
1923
|
+
|
1924
|
+
} // namespace Darts
|
1925
|
+
|
1926
|
+
#undef DARTS_INT_TO_STR
|
1927
|
+
#undef DARTS_LINE_TO_STR
|
1928
|
+
#undef DARTS_LINE_STR
|
1929
|
+
#undef DARTS_THROW
|
1930
|
+
|
1931
|
+
#endif // DARTS_H_
|