1a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek#include "trie.h"
25f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
35f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerextern "C" {
45f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
50bc735ffcfb223c0186419547abaa5c84482663eChris Lattnernamespace {
60bc735ffcfb223c0186419547abaa5c84482663eChris Lattner
75f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerclass FindCallback {
85f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer public:
95f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  typedef int (*Func)(void *, marisa_uint32, size_t);
105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  FindCallback(Func func, void *first_arg)
125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      : func_(func), first_arg_(first_arg) {}
135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  FindCallback(const FindCallback &callback)
145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      : func_(callback.func_), first_arg_(callback.first_arg_) {}
154985aceceb9b9261b876b515d32726175c13a775Steve Naroff
163b1191d7eaf2f4984564e01ab84b6713a9d80e70Fariborz Jahanian  bool operator()(marisa::UInt32 key_id, std::size_t key_length) const {
17a55e52c0802cae3b7c366a05c461d3d15074c1a3Sebastian Redl    return func_(first_arg_, key_id, key_length) != 0;
18500d3297d2a21edeac4d46cbcbe21bc2352c2a28Chris Lattner  }
195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer private:
215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Func func_;
225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *first_arg_;
23891dca671a80c865c6def7259f170ba785e4faaaChris Lattner
245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Disallows assignment.
255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  FindCallback &operator=(const FindCallback &);
2691fa0b7759b575045142abd6ee48c076297ad77bSteve Naroff};
2791fa0b7759b575045142abd6ee48c076297ad77bSteve Naroff
2891fa0b7759b575045142abd6ee48c076297ad77bSteve Naroffclass PredictCallback {
2991fa0b7759b575045142abd6ee48c076297ad77bSteve Naroff public:
3091fa0b7759b575045142abd6ee48c076297ad77bSteve Naroff  typedef int (*Func)(void *, marisa_uint32, const char *, size_t);
31dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff
325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  PredictCallback(Func func, void *first_arg)
335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      : func_(func), first_arg_(first_arg) {}
34861cf3effdc0fbc97d401539bc3050da76b2476fSteve Naroff  PredictCallback(const PredictCallback &callback)
355ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner      : func_(callback.func_), first_arg_(callback.first_arg_) {}
365ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner
375ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner  bool operator()(marisa::UInt32 key_id, const std::string &key) const {
385ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner    return func_(first_arg_, key_id, key.c_str(), key.length()) != 0;
395ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner  }
405ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner
415ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner private:
425ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner  Func func_;
435ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner  void *first_arg_;
445ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner
455ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner  // Disallows assignment.
465ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner  PredictCallback &operator=(const PredictCallback &);
475ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner};
485ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner
495ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner}  // namespace
505ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner
515ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattnerstruct marisa_trie_ {
525ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner public:
535ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner  marisa_trie_() : trie(), mapper() {}
545ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner
555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  marisa::Trie trie;
565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  marisa::Mapper mapper;
575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer private:
595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Disallows copy and assignment.
605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  marisa_trie_(const marisa_trie_ &);
615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  marisa_trie_ &operator=(const marisa_trie_ &);
62dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff};
635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencermarisa_status marisa_init(marisa_trie **h) {
655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if ((h == NULL) || (*h != NULL)) {
665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return MARISA_HANDLE_ERROR;
67df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  }
685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  *h = new (std::nothrow) marisa_trie_();
695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return (*h != NULL) ? MARISA_OK : MARISA_MEMORY_ERROR;
70dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff}
715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencermarisa_status marisa_end(marisa_trie *h) {
735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (h == NULL) {
745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return MARISA_HANDLE_ERROR;
75df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  }
765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  delete h;
775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return MARISA_OK;
785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencermarisa_status marisa_build(marisa_trie *h, const char * const *keys,
815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    size_t num_keys, const size_t *key_lengths, const double *key_weights,
825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    marisa_uint32 *key_ids, int flags) {
83dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  if (h == NULL) {
845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return MARISA_HANDLE_ERROR;
85e440eb8158e71deb1e4ab11618ae3d680aac6da1Steve Naroff  }
863536b443bc50d58a79f14fca9b6842541a434854Steve Naroff  h->trie.build(keys, num_keys, key_lengths, key_weights, key_ids, flags);
875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  h->mapper.clear();
885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return MARISA_OK;
89dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff}
90dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff
91dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroffmarisa_status marisa_mmap(marisa_trie *h, const char *filename,
92dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    long offset, int whence) {
93dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  if (h == NULL) {
94dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    return MARISA_HANDLE_ERROR;
95dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  }
96dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  h->trie.mmap(&h->mapper, filename, offset, whence);
97dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  return MARISA_OK;
98dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff}
99dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff
100dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroffmarisa_status marisa_map(marisa_trie *h, const void *ptr, size_t size) {
101dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  if (h == NULL) {
102dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    return MARISA_HANDLE_ERROR;
103dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  }
104dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  h->trie.map(ptr, size);
105dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  h->mapper.clear();
106dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  return MARISA_OK;
107dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff}
108dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff
109dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroffmarisa_status marisa_load(marisa_trie *h, const char *filename,
110dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    long offset, int whence) {
111dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  if (h == NULL) {
112dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    return MARISA_HANDLE_ERROR;
113dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  }
114dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  h->trie.load(filename, offset, whence);
115dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  h->mapper.clear();
116dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  return MARISA_OK;
117dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff}
118dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff
119861cf3effdc0fbc97d401539bc3050da76b2476fSteve Naroffmarisa_status marisa_fread(marisa_trie *h, FILE *file) {
120dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  if (h == NULL) {
121dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    return MARISA_HANDLE_ERROR;
122dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  }
123df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  h->trie.fread(file);
124dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  h->mapper.clear();
125dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  return MARISA_OK;
126dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff}
127dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff
1287ef58fdbefddf693910e6403a71b3d367444c897Steve Naroffmarisa_status marisa_read(marisa_trie *h, int fd) {
129dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  if (h == NULL) {
130dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    return MARISA_HANDLE_ERROR;
131df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  }
132dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  h->trie.read(fd);
133dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  h->mapper.clear();
134dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  return MARISA_OK;
135dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff}
136527fe23f8cbc6a4da1737a547a2517bc92fa88c8Steve Naroff
137df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattnermarisa_status marisa_save(const marisa_trie *h, const char *filename,
138dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    int trunc_flag, long offset, int whence) {
139dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  if (h == NULL) {
140527fe23f8cbc6a4da1737a547a2517bc92fa88c8Steve Naroff    return MARISA_HANDLE_ERROR;
141527fe23f8cbc6a4da1737a547a2517bc92fa88c8Steve Naroff  }
142527fe23f8cbc6a4da1737a547a2517bc92fa88c8Steve Naroff  h->trie.save(filename, trunc_flag != 0, offset, whence);
143dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  return MARISA_OK;
144df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner}
145dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff
146dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroffmarisa_status marisa_fwrite(const marisa_trie *h, FILE *file) {
147dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  if (h == NULL) {
148dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    return MARISA_HANDLE_ERROR;
149dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  }
1506bd6d0b9d8944c5e192097bef24f2becb83af172Chris Lattner  h->trie.fwrite(file);
151dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  return MARISA_OK;
1526bd6d0b9d8944c5e192097bef24f2becb83af172Chris Lattner}
1536bd6d0b9d8944c5e192097bef24f2becb83af172Chris Lattner
1546bd6d0b9d8944c5e192097bef24f2becb83af172Chris Lattnermarisa_status marisa_write(const marisa_trie *h, int fd) {
1556bd6d0b9d8944c5e192097bef24f2becb83af172Chris Lattner  if (h == NULL) {
1566bd6d0b9d8944c5e192097bef24f2becb83af172Chris Lattner    return MARISA_HANDLE_ERROR;
1576bd6d0b9d8944c5e192097bef24f2becb83af172Chris Lattner  }
158dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  h->trie.write(fd);
159dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  return MARISA_OK;
160dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff}
161e440eb8158e71deb1e4ab11618ae3d680aac6da1Steve Naroff
1623a165b066e665870c163af64844073a664c042d0Steve Naroffmarisa_status marisa_restore(const marisa_trie *h, marisa_uint32 key_id,
163423cb565abc681b770fb4b9b4bc24d398c98157bSteve Naroff    char *key_buf, size_t key_buf_size, size_t *key_length) {
1646bd6d0b9d8944c5e192097bef24f2becb83af172Chris Lattner  if (h == NULL) {
165fd225cc227143553898f2d3902242d25db9a4902Fariborz Jahanian    return MARISA_HANDLE_ERROR;
166fd225cc227143553898f2d3902242d25db9a4902Fariborz Jahanian  } else if (key_length == NULL) {
167bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner    return MARISA_PARAM_ERROR;
168dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  }
169dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  *key_length = h->trie.restore(key_id, key_buf, key_buf_size);
170dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  return MARISA_OK;
171dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff}
1727ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff
173df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattnermarisa_status marisa_lookup(const marisa_trie *h,
174dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    const char *ptr, size_t length, marisa_uint32 *key_id) {
175df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (h == NULL) {
176dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    return MARISA_HANDLE_ERROR;
177dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  } else if (key_id == NULL) {
178dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    return MARISA_PARAM_ERROR;
179dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  }
180dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  if (length == MARISA_ZERO_TERMINATED) {
181dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    *key_id = h->trie.lookup(ptr);
182dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  } else {
18306036d3709955a53297b4cbe14e20db88f321470Chris Lattner    *key_id = h->trie.lookup(ptr, length);
18406036d3709955a53297b4cbe14e20db88f321470Chris Lattner  }
18506036d3709955a53297b4cbe14e20db88f321470Chris Lattner  return MARISA_OK;
18606036d3709955a53297b4cbe14e20db88f321470Chris Lattner}
18706036d3709955a53297b4cbe14e20db88f321470Chris Lattner
18806036d3709955a53297b4cbe14e20db88f321470Chris Lattnermarisa_status marisa_find(const marisa_trie *h,
18906036d3709955a53297b4cbe14e20db88f321470Chris Lattner    const char *ptr, size_t length,
19006036d3709955a53297b4cbe14e20db88f321470Chris Lattner    marisa_uint32 *key_ids, size_t *key_lengths,
19106036d3709955a53297b4cbe14e20db88f321470Chris Lattner    size_t max_num_results, size_t *num_results) {
19206036d3709955a53297b4cbe14e20db88f321470Chris Lattner  if (h == NULL) {
19306036d3709955a53297b4cbe14e20db88f321470Chris Lattner    return MARISA_HANDLE_ERROR;
194f28b264437053fb0deacc9ba02b18a0966f7290aSteve Naroff  } else if (num_results == NULL) {
195df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    return MARISA_PARAM_ERROR;
19660fcceeedbfc8b4a99cb942e2bc5aeb9e2f92a1fSteve Naroff  }
197dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  if (length == MARISA_ZERO_TERMINATED) {
19825e077d59a8e8e43b65882b69610a3d5e2aaf53cFariborz Jahanian    *num_results = h->trie.find(ptr, key_ids, key_lengths, max_num_results);
199bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner  } else {
200dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    *num_results = h->trie.find(ptr, length,
201dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff        key_ids, key_lengths, max_num_results);
2024d7da2f4a39adcc2ea211fb30600fabde4f3a0b1Daniel Dunbar  }
2034d7da2f4a39adcc2ea211fb30600fabde4f3a0b1Daniel Dunbar  return MARISA_OK;
2044d7da2f4a39adcc2ea211fb30600fabde4f3a0b1Daniel Dunbar}
2054d7da2f4a39adcc2ea211fb30600fabde4f3a0b1Daniel Dunbar
2064d7da2f4a39adcc2ea211fb30600fabde4f3a0b1Daniel Dunbarmarisa_status marisa_find_first(const marisa_trie *h,
207edc66f3ab000f54e0e03c5840d7b54e9bf8615cbChris Lattner    const char *ptr, size_t length,
20869d27b9f2e1c678b6b8a199fa3f705f2b3ff5e0dChris Lattner    marisa_uint32 *key_id, size_t *key_length) {
209edc66f3ab000f54e0e03c5840d7b54e9bf8615cbChris Lattner  if (h == NULL) {
2104d7da2f4a39adcc2ea211fb30600fabde4f3a0b1Daniel Dunbar    return MARISA_HANDLE_ERROR;
211edc66f3ab000f54e0e03c5840d7b54e9bf8615cbChris Lattner  } else if (key_id == NULL) {
2124d7da2f4a39adcc2ea211fb30600fabde4f3a0b1Daniel Dunbar    return MARISA_PARAM_ERROR;
2134d7da2f4a39adcc2ea211fb30600fabde4f3a0b1Daniel Dunbar  }
214dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  if (length == MARISA_ZERO_TERMINATED) {
215dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    *key_id = h->trie.find_first(ptr, key_length);
216dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  } else {
217294494e1cce92043562b4680c613df7fd028c02eSteve Naroff    *key_id = h->trie.find_first(ptr, length, key_length);
2183536b443bc50d58a79f14fca9b6842541a434854Steve Naroff  }
219dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  return MARISA_OK;
220dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff}
221dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff
222294494e1cce92043562b4680c613df7fd028c02eSteve Naroffmarisa_status marisa_find_last(const marisa_trie *h,
223294494e1cce92043562b4680c613df7fd028c02eSteve Naroff    const char *ptr, size_t length,
224294494e1cce92043562b4680c613df7fd028c02eSteve Naroff    marisa_uint32 *key_id, size_t *key_length) {
225294494e1cce92043562b4680c613df7fd028c02eSteve Naroff  if (h == NULL) {
22625e077d59a8e8e43b65882b69610a3d5e2aaf53cFariborz Jahanian    return MARISA_HANDLE_ERROR;
227cb53b361bce341c8591333c6997f62e480acc0b4Chris Lattner  } else if (key_id == NULL) {
2288a779314870760848e61da2c428a78971fe3f1c3Ted Kremenek    return MARISA_PARAM_ERROR;
22982a5fe3d1cf204b672cdab24d72275b6ad2c3527Fariborz Jahanian  }
23000933591a2795d09dd1acff12a2d21bce7cb12c5Fariborz Jahanian  if (length == MARISA_ZERO_TERMINATED) {
23160fcceeedbfc8b4a99cb942e2bc5aeb9e2f92a1fSteve Naroff    *key_id = h->trie.find_last(ptr, key_length);
232bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner  } else {
233bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner    *key_id = h->trie.find_last(ptr, length, key_length);
234294494e1cce92043562b4680c613df7fd028c02eSteve Naroff  }
235e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner  return MARISA_OK;
236df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner}
237df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner
238df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattnermarisa_status marisa_find_callback(const marisa_trie *h,
23925e077d59a8e8e43b65882b69610a3d5e2aaf53cFariborz Jahanian    const char *ptr, size_t length,
2403536b443bc50d58a79f14fca9b6842541a434854Steve Naroff    int (*callback)(void *, marisa_uint32, size_t),
2413536b443bc50d58a79f14fca9b6842541a434854Steve Naroff    void *first_arg_to_callback) {
242b6d74a158a9c002e3c0fcbda8ad8d0ccbb2e5088Chris Lattner  if (h == NULL) {
243b6d74a158a9c002e3c0fcbda8ad8d0ccbb2e5088Chris Lattner    return MARISA_HANDLE_ERROR;
244294494e1cce92043562b4680c613df7fd028c02eSteve Naroff  } else if (callback == NULL) {
245294494e1cce92043562b4680c613df7fd028c02eSteve Naroff    return MARISA_PARAM_ERROR;
246f366b4c84485b646c30d6891a249ad6f65abef7cFariborz Jahanian  }
247e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner  if (length == MARISA_ZERO_TERMINATED) {
248e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    h->trie.find_callback(ptr,
249294494e1cce92043562b4680c613df7fd028c02eSteve Naroff        ::FindCallback(callback, first_arg_to_callback));
250e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner  } else {
251e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    h->trie.find_callback(ptr, length,
252e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner        ::FindCallback(callback, first_arg_to_callback));
253bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner  }
254e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner  return MARISA_OK;
255e3a2ca7e30601cdd31c77a830f4cc487851e8096Fariborz Jahanian}
256e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner
257e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattnermarisa_status marisa_predict(const marisa_trie *h,
258e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    const char *ptr, size_t length, marisa_uint32 *key_ids,
2591fd80116b49782c367ff5d5f50a8b76dd8a5d7f7Chris Lattner    size_t max_num_results, size_t *num_results) {
2601fd80116b49782c367ff5d5f50a8b76dd8a5d7f7Chris Lattner  return marisa_predict_breadth_first(h, ptr, length,
2611fd80116b49782c367ff5d5f50a8b76dd8a5d7f7Chris Lattner      key_ids, max_num_results, num_results);
2621fd80116b49782c367ff5d5f50a8b76dd8a5d7f7Chris Lattner}
2631fd80116b49782c367ff5d5f50a8b76dd8a5d7f7Chris Lattner
2641fd80116b49782c367ff5d5f50a8b76dd8a5d7f7Chris Lattnermarisa_status marisa_predict_breadth_first(const marisa_trie *h,
2654985aceceb9b9261b876b515d32726175c13a775Steve Naroff    const char *ptr, size_t length, marisa_uint32 *key_ids,
2664985aceceb9b9261b876b515d32726175c13a775Steve Naroff    size_t max_num_results, size_t *num_results) {
2673536b443bc50d58a79f14fca9b6842541a434854Steve Naroff  if (h == NULL) {
268e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    return MARISA_HANDLE_ERROR;
269e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner  } else if (num_results == NULL) {
270e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    return MARISA_PARAM_ERROR;
271e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner  }
272e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner  if (length == MARISA_ZERO_TERMINATED) {
273a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner    *num_results = h->trie.predict_breadth_first(
274e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner        ptr, key_ids, NULL, max_num_results);
275a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner  } else {
276e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    *num_results = h->trie.predict_breadth_first(
277e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner        ptr, length, key_ids, NULL, max_num_results);
278bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner  }
279e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner  return MARISA_OK;
280bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner}
281bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner
282bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattnermarisa_status marisa_predict_depth_first(const marisa_trie *h,
283a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner    const char *ptr, size_t length, marisa_uint32 *key_ids,
284a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner    size_t max_num_results, size_t *num_results) {
285bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner  if (h == NULL) {
286bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner    return MARISA_HANDLE_ERROR;
287bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner  } else if (num_results == NULL) {
288bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner    return MARISA_PARAM_ERROR;
289f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner  }
290bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner  if (length == MARISA_ZERO_TERMINATED) {
291a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner    *num_results = h->trie.predict_depth_first(
292a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner        ptr, key_ids, NULL, max_num_results);
293a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner  } else {
294a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner    *num_results = h->trie.predict_depth_first(
295a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner        ptr, length, key_ids, NULL, max_num_results);
296a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner  }
297bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner  return MARISA_OK;
298e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner}
299bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner
300a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattnermarisa_status marisa_predict_callback(const marisa_trie *h,
301bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner    const char *ptr, size_t length,
302a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner    int (*callback)(void *, marisa_uint32, const char *, size_t),
303a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner    void *first_arg_to_callback) {
304a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner  if (h == NULL) {
305f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner    return MARISA_HANDLE_ERROR;
306f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner  } else if (callback == NULL) {
307f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner    return MARISA_PARAM_ERROR;
308e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner  }
309e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner  if (length == MARISA_ZERO_TERMINATED) {
3108ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner    h->trie.predict_callback(ptr,
311e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner        ::PredictCallback(callback, first_arg_to_callback));
312dd5b5f2bb73d037745940431b71eb98393d12d4fChris Lattner  } else {
313e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    h->trie.predict_callback(ptr, length,
314e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner        ::PredictCallback(callback, first_arg_to_callback));
315e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner  }
316e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner  return MARISA_OK;
317e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner}
318a1fed7e3beebf9bb1bc85123f283be3eb631c120Chris Lattner
319a1fed7e3beebf9bb1bc85123f283be3eb631c120Chris Lattnersize_t marisa_get_num_tries(const marisa_trie *h) {
320a1fed7e3beebf9bb1bc85123f283be3eb631c120Chris Lattner  return (h != NULL) ? h->trie.num_tries() : 0;
321e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner}
322e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner
323e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattnersize_t marisa_get_num_keys(const marisa_trie *h) {
324da3253d8a97981257185c89ced71ce137278b121Chris Lattner  return (h != NULL) ? h->trie.num_keys() : 0;
325ef708fd4abccf4d21b1f82ab2ab62f6ae7cc1265Chris Lattner}
326ef708fd4abccf4d21b1f82ab2ab62f6ae7cc1265Chris Lattner
327da3253d8a97981257185c89ced71ce137278b121Chris Lattnersize_t marisa_get_num_nodes(const marisa_trie *h) {
328da3253d8a97981257185c89ced71ce137278b121Chris Lattner  return (h != NULL) ? h->trie.num_nodes() : 0;
329573acde1db5cb3e216e3d63fee173230834093d8Fariborz Jahanian}
330573acde1db5cb3e216e3d63fee173230834093d8Fariborz Jahanian
331573acde1db5cb3e216e3d63fee173230834093d8Fariborz Jahaniansize_t marisa_get_total_size(const marisa_trie *h) {
332573acde1db5cb3e216e3d63fee173230834093d8Fariborz Jahanian  return (h != NULL) ? h->trie.total_size() : 0;
333573acde1db5cb3e216e3d63fee173230834093d8Fariborz Jahanian}
334da3253d8a97981257185c89ced71ce137278b121Chris Lattner
335e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattnermarisa_status marisa_clear(marisa_trie *h) {
336da3253d8a97981257185c89ced71ce137278b121Chris Lattner  if (h == NULL) {
337da3253d8a97981257185c89ced71ce137278b121Chris Lattner    return MARISA_HANDLE_ERROR;
338da3253d8a97981257185c89ced71ce137278b121Chris Lattner  }
339e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner  h->trie.clear();
340da3253d8a97981257185c89ced71ce137278b121Chris Lattner  h->mapper.clear();
341e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner  return MARISA_OK;
342e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner}
343e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner
344e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner}  // extern "C"
345e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner