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
244
245
246
247
248
249
|
#include "stdio.h"
#include "iostream.h"
#include "trie.h"
#include "grimoire/trie.h"
namespace marisa {
Trie::Trie() : trie_() {}
Trie::~Trie() {}
void Trie::build(Keyset &keyset, int config_flags) {
scoped_ptr<grimoire::LoudsTrie> temp(new (std::nothrow) grimoire::LoudsTrie);
MARISA_THROW_IF(temp.get() == NULL, MARISA_MEMORY_ERROR);
temp->build(keyset, config_flags);
trie_.swap(temp);
}
void Trie::mmap(const char *filename) {
MARISA_THROW_IF(filename == NULL, MARISA_NULL_ERROR);
scoped_ptr<grimoire::LoudsTrie> temp(new (std::nothrow) grimoire::LoudsTrie);
MARISA_THROW_IF(temp.get() == NULL, MARISA_MEMORY_ERROR);
grimoire::Mapper mapper;
mapper.open(filename);
temp->map(mapper);
trie_.swap(temp);
}
void Trie::map(const void *ptr, std::size_t size) {
MARISA_THROW_IF((ptr == NULL) && (size != 0), MARISA_NULL_ERROR);
scoped_ptr<grimoire::LoudsTrie> temp(new (std::nothrow) grimoire::LoudsTrie);
MARISA_THROW_IF(temp.get() == NULL, MARISA_MEMORY_ERROR);
grimoire::Mapper mapper;
mapper.open(ptr, size);
temp->map(mapper);
trie_.swap(temp);
}
void Trie::load(const char *filename) {
MARISA_THROW_IF(filename == NULL, MARISA_NULL_ERROR);
scoped_ptr<grimoire::LoudsTrie> temp(new (std::nothrow) grimoire::LoudsTrie);
MARISA_THROW_IF(temp.get() == NULL, MARISA_MEMORY_ERROR);
grimoire::Reader reader;
reader.open(filename);
temp->read(reader);
trie_.swap(temp);
}
void Trie::read(int fd) {
MARISA_THROW_IF(fd == -1, MARISA_CODE_ERROR);
scoped_ptr<grimoire::LoudsTrie> temp(new (std::nothrow) grimoire::LoudsTrie);
MARISA_THROW_IF(temp.get() == NULL, MARISA_MEMORY_ERROR);
grimoire::Reader reader;
reader.open(fd);
temp->read(reader);
trie_.swap(temp);
}
void Trie::save(const char *filename) const {
MARISA_THROW_IF(trie_.get() == NULL, MARISA_STATE_ERROR);
MARISA_THROW_IF(filename == NULL, MARISA_NULL_ERROR);
grimoire::Writer writer;
writer.open(filename);
trie_->write(writer);
}
void Trie::write(int fd) const {
MARISA_THROW_IF(trie_.get() == NULL, MARISA_STATE_ERROR);
MARISA_THROW_IF(fd == -1, MARISA_CODE_ERROR);
grimoire::Writer writer;
writer.open(fd);
trie_->write(writer);
}
bool Trie::lookup(Agent &agent) const {
MARISA_THROW_IF(trie_.get() == NULL, MARISA_STATE_ERROR);
if (!agent.has_state()) {
agent.init_state();
}
return trie_->lookup(agent);
}
void Trie::reverse_lookup(Agent &agent) const {
MARISA_THROW_IF(trie_.get() == NULL, MARISA_STATE_ERROR);
if (!agent.has_state()) {
agent.init_state();
}
trie_->reverse_lookup(agent);
}
bool Trie::common_prefix_search(Agent &agent) const {
MARISA_THROW_IF(trie_.get() == NULL, MARISA_STATE_ERROR);
if (!agent.has_state()) {
agent.init_state();
}
return trie_->common_prefix_search(agent);
}
bool Trie::predictive_search(Agent &agent) const {
MARISA_THROW_IF(trie_.get() == NULL, MARISA_STATE_ERROR);
if (!agent.has_state()) {
agent.init_state();
}
return trie_->predictive_search(agent);
}
std::size_t Trie::num_tries() const {
MARISA_THROW_IF(trie_.get() == NULL, MARISA_STATE_ERROR);
return trie_->num_tries();
}
std::size_t Trie::num_keys() const {
MARISA_THROW_IF(trie_.get() == NULL, MARISA_STATE_ERROR);
return trie_->num_keys();
}
std::size_t Trie::num_nodes() const {
MARISA_THROW_IF(trie_.get() == NULL, MARISA_STATE_ERROR);
return trie_->num_nodes();
}
TailMode Trie::tail_mode() const {
MARISA_THROW_IF(trie_.get() == NULL, MARISA_STATE_ERROR);
return trie_->tail_mode();
}
NodeOrder Trie::node_order() const {
MARISA_THROW_IF(trie_.get() == NULL, MARISA_STATE_ERROR);
return trie_->node_order();
}
bool Trie::empty() const {
MARISA_THROW_IF(trie_.get() == NULL, MARISA_STATE_ERROR);
return trie_->empty();
}
std::size_t Trie::size() const {
MARISA_THROW_IF(trie_.get() == NULL, MARISA_STATE_ERROR);
return trie_->size();
}
std::size_t Trie::total_size() const {
MARISA_THROW_IF(trie_.get() == NULL, MARISA_STATE_ERROR);
return trie_->total_size();
}
std::size_t Trie::io_size() const {
MARISA_THROW_IF(trie_.get() == NULL, MARISA_STATE_ERROR);
return trie_->io_size();
}
void Trie::clear() {
Trie().swap(*this);
}
void Trie::swap(Trie &rhs) {
trie_.swap(rhs.trie_);
}
} // namespace marisa
#include <iostream>
namespace marisa {
class TrieIO {
public:
static void fread(std::FILE *file, Trie *trie) {
MARISA_THROW_IF(trie == NULL, MARISA_NULL_ERROR);
scoped_ptr<grimoire::LoudsTrie> temp(
new (std::nothrow) grimoire::LoudsTrie);
MARISA_THROW_IF(temp.get() == NULL, MARISA_MEMORY_ERROR);
grimoire::Reader reader;
reader.open(file);
temp->read(reader);
trie->trie_.swap(temp);
}
static void fwrite(std::FILE *file, const Trie &trie) {
MARISA_THROW_IF(file == NULL, MARISA_NULL_ERROR);
MARISA_THROW_IF(trie.trie_.get() == NULL, MARISA_STATE_ERROR);
grimoire::Writer writer;
writer.open(file);
trie.trie_->write(writer);
}
static std::istream &read(std::istream &stream, Trie *trie) {
MARISA_THROW_IF(trie == NULL, MARISA_NULL_ERROR);
scoped_ptr<grimoire::LoudsTrie> temp(
new (std::nothrow) grimoire::LoudsTrie);
MARISA_THROW_IF(temp.get() == NULL, MARISA_MEMORY_ERROR);
grimoire::Reader reader;
reader.open(stream);
temp->read(reader);
trie->trie_.swap(temp);
return stream;
}
static std::ostream &write(std::ostream &stream, const Trie &trie) {
MARISA_THROW_IF(trie.trie_.get() == NULL, MARISA_STATE_ERROR);
grimoire::Writer writer;
writer.open(stream);
trie.trie_->write(writer);
return stream;
}
};
void fread(std::FILE *file, Trie *trie) {
MARISA_THROW_IF(file == NULL, MARISA_NULL_ERROR);
MARISA_THROW_IF(trie == NULL, MARISA_NULL_ERROR);
TrieIO::fread(file, trie);
}
void fwrite(std::FILE *file, const Trie &trie) {
MARISA_THROW_IF(file == NULL, MARISA_NULL_ERROR);
TrieIO::fwrite(file, trie);
}
std::istream &read(std::istream &stream, Trie *trie) {
MARISA_THROW_IF(trie == NULL, MARISA_NULL_ERROR);
return TrieIO::read(stream, trie);
}
std::ostream &write(std::ostream &stream, const Trie &trie) {
return TrieIO::write(stream, trie);
}
std::istream &operator>>(std::istream &stream, Trie &trie) {
return read(stream, &trie);
}
std::ostream &operator<<(std::ostream &stream, const Trie &trie) {
return write(stream, trie);
}
} // namespace marisa
|