1 #include <cstring>
2 #include <new>
3 
4 #include "marisa-swig.h"
5 
6 namespace marisa_swig {
7 
str(const char ** ptr_out,size_t * length_out) const8 void Key::str(const char **ptr_out, size_t *length_out) const {
9   *ptr_out = key_.ptr();
10   *length_out = key_.length();
11 }
12 
id() const13 size_t Key::id() const {
14   return key_.id();
15 }
16 
weight() const17 float Key::weight() const {
18   return key_.weight();
19 }
20 
str(const char ** ptr_out,size_t * length_out) const21 void Query::str(const char **ptr_out, size_t *length_out) const {
22   *ptr_out = query_.ptr();
23   *length_out = query_.length();
24 }
25 
id() const26 size_t Query::id() const {
27   return query_.id();
28 }
29 
Keyset()30 Keyset::Keyset() : keyset_(new (std::nothrow) marisa::Keyset) {
31   MARISA_THROW_IF(keyset_ == NULL, ::MARISA_MEMORY_ERROR);
32 }
33 
~Keyset()34 Keyset::~Keyset() {
35   delete keyset_;
36 }
37 
push_back(const marisa::Key & key)38 void Keyset::push_back(const marisa::Key &key) {
39   keyset_->push_back(key);
40 }
41 
push_back(const char * ptr,size_t length,float weight)42 void Keyset::push_back(const char *ptr, size_t length, float weight) {
43   keyset_->push_back(ptr, length, weight);
44 }
45 
key(size_t i) const46 const Key &Keyset::key(size_t i) const {
47   return reinterpret_cast<const Key &>((*keyset_)[i]);
48 }
49 
key_str(size_t i,const char ** ptr_out,size_t * length_out) const50 void Keyset::key_str(size_t i,
51     const char **ptr_out, size_t *length_out) const {
52   *ptr_out = (*keyset_)[i].ptr();
53   *length_out = (*keyset_)[i].length();
54 }
55 
key_id(size_t i) const56 size_t Keyset::key_id(size_t i) const {
57   return (*keyset_)[i].id();
58 }
59 
num_keys() const60 size_t Keyset::num_keys() const {
61   return keyset_->num_keys();
62 }
63 
empty() const64 bool Keyset::empty() const {
65   return keyset_->empty();
66 }
67 
size() const68 size_t Keyset::size() const {
69   return keyset_->size();
70 }
71 
total_length() const72 size_t Keyset::total_length() const {
73   return keyset_->total_length();
74 }
75 
reset()76 void Keyset::reset() {
77   keyset_->reset();
78 }
79 
clear()80 void Keyset::clear() {
81   keyset_->clear();
82 }
83 
Agent()84 Agent::Agent()
85     : agent_(new (std::nothrow) marisa::Agent), buf_(NULL), buf_size_(0) {
86   MARISA_THROW_IF(agent_ == NULL, ::MARISA_MEMORY_ERROR);
87 }
88 
~Agent()89 Agent::~Agent() {
90   delete agent_;
91   delete [] buf_;
92 }
93 
set_query(const char * ptr,size_t length)94 void Agent::set_query(const char *ptr, size_t length) {
95   if (length > buf_size_) {
96     size_t new_buf_size = (buf_size_ != 0) ? buf_size_ : 1;
97     if (length >= (MARISA_SIZE_MAX / 2)) {
98       new_buf_size = MARISA_SIZE_MAX;
99     } else {
100       while (new_buf_size < length) {
101         new_buf_size *= 2;
102       }
103     }
104     char *new_buf = new (std::nothrow) char[new_buf_size];
105     MARISA_THROW_IF(new_buf == NULL, MARISA_MEMORY_ERROR);
106     delete [] buf_;
107     buf_ = new_buf;
108     buf_size_ = new_buf_size;
109   }
110   std::memcpy(buf_, ptr, length);
111   agent_->set_query(buf_, length);
112 }
113 
set_query(size_t id)114 void Agent::set_query(size_t id) {
115   agent_->set_query(id);
116 }
117 
key() const118 const Key &Agent::key() const {
119   return reinterpret_cast<const Key &>(agent_->key());
120 }
121 
query() const122 const Query &Agent::query() const {
123   return reinterpret_cast<const Query &>(agent_->query());
124 }
125 
key_str(const char ** ptr_out,size_t * length_out) const126 void Agent::key_str(const char **ptr_out, size_t *length_out) const {
127   *ptr_out = agent_->key().ptr();
128   *length_out = agent_->key().length();
129 }
130 
key_id() const131 size_t Agent::key_id() const {
132   return agent_->key().id();
133 }
134 
query_str(const char ** ptr_out,size_t * length_out) const135 void Agent::query_str(const char **ptr_out, size_t *length_out) const {
136   *ptr_out = agent_->query().ptr();
137   *length_out = agent_->query().length();
138 }
139 
query_id() const140 size_t Agent::query_id() const {
141   return agent_->query().id();
142 }
143 
Trie()144 Trie::Trie() : trie_(new (std::nothrow) marisa::Trie) {
145   MARISA_THROW_IF(trie_ == NULL, ::MARISA_MEMORY_ERROR);
146 }
147 
~Trie()148 Trie::~Trie() {
149   delete trie_;
150 }
151 
build(Keyset & keyset,int config_flags)152 void Trie::build(Keyset &keyset, int config_flags) {
153   trie_->build(*keyset.keyset_, config_flags);
154 }
155 
mmap(const char * filename)156 void Trie::mmap(const char *filename) {
157   trie_->mmap(filename);
158 }
159 
load(const char * filename)160 void Trie::load(const char *filename) {
161   trie_->load(filename);
162 }
163 
save(const char * filename) const164 void Trie::save(const char *filename) const {
165   trie_->save(filename);
166 }
167 
lookup(Agent & agent) const168 bool Trie::lookup(Agent &agent) const {
169   return trie_->lookup(*agent.agent_);
170 }
171 
reverse_lookup(Agent & agent) const172 void Trie::reverse_lookup(Agent &agent) const {
173   trie_->reverse_lookup(*agent.agent_);
174 }
175 
common_prefix_search(Agent & agent) const176 bool Trie::common_prefix_search(Agent &agent) const {
177   return trie_->common_prefix_search(*agent.agent_);
178 }
179 
predictive_search(Agent & agent) const180 bool Trie::predictive_search(Agent &agent) const {
181   return trie_->predictive_search(*agent.agent_);
182 }
183 
lookup(const char * ptr,size_t length) const184 size_t Trie::lookup(const char *ptr, size_t length) const {
185   marisa::Agent agent;
186   agent.set_query(ptr, length);
187   if (!trie_->lookup(agent)) {
188     return MARISA_INVALID_KEY_ID;
189   }
190   return agent.key().id();
191 }
192 
reverse_lookup(size_t id,const char ** ptr_out_to_be_deleted,size_t * length_out) const193 void Trie::reverse_lookup(size_t id,
194     const char **ptr_out_to_be_deleted, size_t *length_out) const {
195   marisa::Agent agent;
196   agent.set_query(id);
197   trie_->reverse_lookup(agent);
198   char * const buf = new (std::nothrow) char[agent.key().length()];
199   MARISA_THROW_IF(buf == NULL, MARISA_MEMORY_ERROR);
200   std::memcpy(buf, agent.key().ptr(), agent.key().length());
201   *ptr_out_to_be_deleted = buf;
202   *length_out = agent.key().length();
203 }
204 
num_tries() const205 size_t Trie::num_tries() const {
206   return trie_->num_tries();
207 }
208 
num_keys() const209 size_t Trie::num_keys() const {
210   return trie_->num_keys();
211 }
212 
num_nodes() const213 size_t Trie::num_nodes() const {
214   return trie_->num_nodes();
215 }
216 
tail_mode() const217 TailMode Trie::tail_mode() const {
218   if (trie_->tail_mode() == ::MARISA_TEXT_TAIL) {
219     return TEXT_TAIL;
220   } else {
221     return BINARY_TAIL;
222   }
223 }
224 
node_order() const225 NodeOrder Trie::node_order() const {
226   if (trie_->node_order() == ::MARISA_LABEL_ORDER) {
227     return LABEL_ORDER;
228   } else {
229     return WEIGHT_ORDER;
230   }
231 }
232 
empty() const233 bool Trie::empty() const {
234   return trie_->empty();
235 }
236 
size() const237 size_t Trie::size() const {
238   return trie_->size();
239 }
240 
total_size() const241 size_t Trie::total_size() const {
242   return trie_->total_size();
243 }
244 
io_size() const245 size_t Trie::io_size() const {
246   return trie_->io_size();
247 }
248 
clear()249 void Trie::clear() {
250   trie_->clear();
251 }
252 
253 }  // namespace marisa_swig
254