1b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak#include "trie.h" 2b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak 3b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniakextern "C" { 4b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak 5b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniaknamespace { 6b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak 7b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniakclass FindCallback { 8b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak public: 9b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak typedef int (*Func)(void *, marisa_alpha_uint32, size_t); 10b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak 11b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak FindCallback(Func func, void *first_arg) 12b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak : func_(func), first_arg_(first_arg) {} 13b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak FindCallback(const FindCallback &callback) 14b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak : func_(callback.func_), first_arg_(callback.first_arg_) {} 15b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak 16b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak bool operator()(marisa_alpha::UInt32 key_id, std::size_t key_length) const { 17b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak return func_(first_arg_, key_id, key_length) != 0; 18b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak } 19b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak 20b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak private: 21b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak Func func_; 22b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak void *first_arg_; 23b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak 24b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak // Disallows assignment. 25b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak FindCallback &operator=(const FindCallback &); 26b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak}; 27b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak 28b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniakclass PredictCallback { 29b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak public: 30b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak typedef int (*Func)(void *, marisa_alpha_uint32, const char *, size_t); 31b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak 32b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak PredictCallback(Func func, void *first_arg) 33b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak : func_(func), first_arg_(first_arg) {} 34b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak PredictCallback(const PredictCallback &callback) 35b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak : func_(callback.func_), first_arg_(callback.first_arg_) {} 36b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak 37b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak bool operator()(marisa_alpha::UInt32 key_id, const std::string &key) const { 38b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak return func_(first_arg_, key_id, key.c_str(), key.length()) != 0; 39b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak } 40b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak 41b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak private: 42b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak Func func_; 43b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak void *first_arg_; 44b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak 45b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak // Disallows assignment. 46b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak PredictCallback &operator=(const PredictCallback &); 47b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak}; 48b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak 49b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak} // namespace 50b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak 51b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniakstruct marisa_alpha_trie_ { 52b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak public: 53b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak marisa_alpha_trie_() : trie(), mapper() {} 54b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak 55b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak marisa_alpha::Trie trie; 56b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak marisa_alpha::Mapper mapper; 57b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak 58b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak private: 59b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak // Disallows copy and assignment. 60b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak marisa_alpha_trie_(const marisa_alpha_trie_ &); 61b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak marisa_alpha_trie_ &operator=(const marisa_alpha_trie_ &); 62b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak}; 63b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak 64b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniakmarisa_alpha_status marisa_alpha_init(marisa_alpha_trie **h) { 65b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak if ((h == NULL) || (*h != NULL)) { 66b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak return MARISA_ALPHA_HANDLE_ERROR; 67b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak } 68b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak *h = new (std::nothrow) marisa_alpha_trie_(); 69b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak return (*h != NULL) ? MARISA_ALPHA_OK : MARISA_ALPHA_MEMORY_ERROR; 70b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak} 71b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak 72b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniakmarisa_alpha_status marisa_alpha_end(marisa_alpha_trie *h) { 73b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak if (h == NULL) { 74b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak return MARISA_ALPHA_HANDLE_ERROR; 75b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak } 76b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak delete h; 77b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak return MARISA_ALPHA_OK; 78b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak} 79b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak 80b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniakmarisa_alpha_status marisa_alpha_build(marisa_alpha_trie *h, 81b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak const char * const *keys, size_t num_keys, const size_t *key_lengths, 82b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak const double *key_weights, marisa_alpha_uint32 *key_ids, int flags) try { 83b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak if (h == NULL) { 84b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak return MARISA_ALPHA_HANDLE_ERROR; 85b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak } 86b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak h->trie.build(keys, num_keys, key_lengths, key_weights, key_ids, flags); 87b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak h->mapper.clear(); 88b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak return MARISA_ALPHA_OK; 89b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak} catch (const marisa_alpha::Exception &ex) { 90b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak return ex.status(); 91b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak} 92b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak 93b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniakmarisa_alpha_status marisa_alpha_mmap(marisa_alpha_trie *h, 94b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak const char *filename, long offset, int whence) try { 95b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak if (h == NULL) { 96b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak return MARISA_ALPHA_HANDLE_ERROR; 97b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak } 98b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak h->trie.mmap(&h->mapper, filename, offset, whence); 99b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak return MARISA_ALPHA_OK; 100b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak} catch (const marisa_alpha::Exception &ex) { 101b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak return ex.status(); 102b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak} 103b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak 104b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniakmarisa_alpha_status marisa_alpha_map(marisa_alpha_trie *h, const void *ptr, 105b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak size_t size) try { 106b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak if (h == NULL) { 107b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak return MARISA_ALPHA_HANDLE_ERROR; 108b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak } 109b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak h->trie.map(ptr, size); 110b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak h->mapper.clear(); 111b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak return MARISA_ALPHA_OK; 112b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak} catch (const marisa_alpha::Exception &ex) { 113b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak return ex.status(); 114b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak} 115b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak 116b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniakmarisa_alpha_status marisa_alpha_load(marisa_alpha_trie *h, 117b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak const char *filename, long offset, int whence) try { 118b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak if (h == NULL) { 119b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak return MARISA_ALPHA_HANDLE_ERROR; 120b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak } 121b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak h->trie.load(filename, offset, whence); 122b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak h->mapper.clear(); 123b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak return MARISA_ALPHA_OK; 124b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak} catch (const marisa_alpha::Exception &ex) { 125b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak return ex.status(); 126b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak} 127b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak 128b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniakmarisa_alpha_status marisa_alpha_fread(marisa_alpha_trie *h, FILE *file) try { 129b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak if (h == NULL) { 130b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak return MARISA_ALPHA_HANDLE_ERROR; 131b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak } 132b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak h->trie.fread(file); 133b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak h->mapper.clear(); 134b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak return MARISA_ALPHA_OK; 135b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak} catch (const marisa_alpha::Exception &ex) { 136b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak return ex.status(); 137b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak} 138b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak 139b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniakmarisa_alpha_status marisa_alpha_read(marisa_alpha_trie *h, int fd) try { 140b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak if (h == NULL) { 141b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak return MARISA_ALPHA_HANDLE_ERROR; 142b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak } 143b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak h->trie.read(fd); 144b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak h->mapper.clear(); 145b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak return MARISA_ALPHA_OK; 146b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak} catch (const marisa_alpha::Exception &ex) { 147b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak return ex.status(); 148b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak} 149b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak 150b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniakmarisa_alpha_status marisa_alpha_save(const marisa_alpha_trie *h, 151b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak const char *filename, int trunc_flag, long offset, int whence) try { 152b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak if (h == NULL) { 153b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak return MARISA_ALPHA_HANDLE_ERROR; 154b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak } 155b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak h->trie.save(filename, trunc_flag != 0, offset, whence); 156b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak return MARISA_ALPHA_OK; 157b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak} catch (const marisa_alpha::Exception &ex) { 158b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak return ex.status(); 159b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak} 160b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak 161b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniakmarisa_alpha_status marisa_alpha_fwrite(const marisa_alpha_trie *h, 162b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak FILE *file) try { 163b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak if (h == NULL) { 164b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak return MARISA_ALPHA_HANDLE_ERROR; 165b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak } 166b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak h->trie.fwrite(file); 167b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak return MARISA_ALPHA_OK; 168b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak} catch (const marisa_alpha::Exception &ex) { 169b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak return ex.status(); 170b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak} 171b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak 172b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniakmarisa_alpha_status marisa_alpha_write(const marisa_alpha_trie *h, int fd) try { 173b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak if (h == NULL) { 174b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak return MARISA_ALPHA_HANDLE_ERROR; 175b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak } 176b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak h->trie.write(fd); 177b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak return MARISA_ALPHA_OK; 178b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak} catch (const marisa_alpha::Exception &ex) { 179b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak return ex.status(); 180b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak} 181b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak 182b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniakmarisa_alpha_status marisa_alpha_restore(const marisa_alpha_trie *h, 183b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak marisa_alpha_uint32 key_id, char *key_buf, size_t key_buf_size, 184b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak size_t *key_length) try { 185b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak if (h == NULL) { 186b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak return MARISA_ALPHA_HANDLE_ERROR; 187b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak } else if (key_length == NULL) { 188b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak return MARISA_ALPHA_PARAM_ERROR; 189b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak } 190b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak *key_length = h->trie.restore(key_id, key_buf, key_buf_size); 191b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak return MARISA_ALPHA_OK; 192b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak} catch (const marisa_alpha::Exception &ex) { 193b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak return ex.status(); 194b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak} 195b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak 196b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniakmarisa_alpha_status marisa_alpha_lookup(const marisa_alpha_trie *h, 197b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak const char *ptr, size_t length, marisa_alpha_uint32 *key_id) try { 198b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak if (h == NULL) { 199b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak return MARISA_ALPHA_HANDLE_ERROR; 200b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak } else if (key_id == NULL) { 201b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak return MARISA_ALPHA_PARAM_ERROR; 202b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak } 203b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak if (length == MARISA_ALPHA_ZERO_TERMINATED) { 204b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak *key_id = h->trie.lookup(ptr); 205b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak } else { 206b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak *key_id = h->trie.lookup(ptr, length); 207b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak } 208b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak return MARISA_ALPHA_OK; 209b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak} catch (const marisa_alpha::Exception &ex) { 210b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak return ex.status(); 211b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak} 212b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak 213b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniakmarisa_alpha_status marisa_alpha_find(const marisa_alpha_trie *h, 214b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak const char *ptr, size_t length, 215b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak marisa_alpha_uint32 *key_ids, size_t *key_lengths, 216b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak size_t max_num_results, size_t *num_results) try { 217b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak if (h == NULL) { 218b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak return MARISA_ALPHA_HANDLE_ERROR; 219b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak } else if (num_results == NULL) { 220b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak return MARISA_ALPHA_PARAM_ERROR; 221b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak } 222b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak if (length == MARISA_ALPHA_ZERO_TERMINATED) { 223b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak *num_results = h->trie.find(ptr, key_ids, key_lengths, max_num_results); 224b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak } else { 225b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak *num_results = h->trie.find(ptr, length, 226b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak key_ids, key_lengths, max_num_results); 227b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak } 228b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak return MARISA_ALPHA_OK; 229b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak} catch (const marisa_alpha::Exception &ex) { 230b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak return ex.status(); 231b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak} 232b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak 233b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniakmarisa_alpha_status marisa_alpha_find_first(const marisa_alpha_trie *h, 234b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak const char *ptr, size_t length, 235b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak marisa_alpha_uint32 *key_id, size_t *key_length) { 236b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak if (h == NULL) { 237b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak return MARISA_ALPHA_HANDLE_ERROR; 238b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak } else if (key_id == NULL) { 239b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak return MARISA_ALPHA_PARAM_ERROR; 240b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak } 241b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak if (length == MARISA_ALPHA_ZERO_TERMINATED) { 242b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak *key_id = h->trie.find_first(ptr, key_length); 243b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak } else { 244b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak *key_id = h->trie.find_first(ptr, length, key_length); 245b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak } 246b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak return MARISA_ALPHA_OK; 247b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak} 248b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak 249b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniakmarisa_alpha_status marisa_alpha_find_last(const marisa_alpha_trie *h, 250b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak const char *ptr, size_t length, 251b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak marisa_alpha_uint32 *key_id, size_t *key_length) { 252b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak if (h == NULL) { 253b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak return MARISA_ALPHA_HANDLE_ERROR; 254b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak } else if (key_id == NULL) { 255b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak return MARISA_ALPHA_PARAM_ERROR; 256b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak } 257b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak if (length == MARISA_ALPHA_ZERO_TERMINATED) { 258b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak *key_id = h->trie.find_last(ptr, key_length); 259b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak } else { 260b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak *key_id = h->trie.find_last(ptr, length, key_length); 261b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak } 262b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak return MARISA_ALPHA_OK; 263b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak} 264b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak 265b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniakmarisa_alpha_status marisa_alpha_find_callback(const marisa_alpha_trie *h, 266b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak const char *ptr, size_t length, 267b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak int (*callback)(void *, marisa_alpha_uint32, size_t), 268b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak void *first_arg_to_callback) try { 269b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak if (h == NULL) { 270b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak return MARISA_ALPHA_HANDLE_ERROR; 271b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak } else if (callback == NULL) { 272b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak return MARISA_ALPHA_PARAM_ERROR; 273b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak } 274b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak if (length == MARISA_ALPHA_ZERO_TERMINATED) { 275b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak h->trie.find_callback(ptr, 276b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak ::FindCallback(callback, first_arg_to_callback)); 277b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak } else { 278b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak h->trie.find_callback(ptr, length, 279b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak ::FindCallback(callback, first_arg_to_callback)); 280b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak } 281b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak return MARISA_ALPHA_OK; 282b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak} catch (const marisa_alpha::Exception &ex) { 283b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak return ex.status(); 284b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak} 285b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak 286b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniakmarisa_alpha_status marisa_alpha_predict(const marisa_alpha_trie *h, 287b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak const char *ptr, size_t length, marisa_alpha_uint32 *key_ids, 288b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak size_t max_num_results, size_t *num_results) { 289b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak return marisa_alpha_predict_breadth_first(h, ptr, length, 290b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak key_ids, max_num_results, num_results); 291b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak} 292b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak 293b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniakmarisa_alpha_status marisa_alpha_predict_breadth_first( 294b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak const marisa_alpha_trie *h, const char *ptr, size_t length, 295b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak marisa_alpha_uint32 *key_ids, size_t max_num_results, 296b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak size_t *num_results) try { 297b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak if (h == NULL) { 298b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak return MARISA_ALPHA_HANDLE_ERROR; 299b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak } else if (num_results == NULL) { 300b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak return MARISA_ALPHA_PARAM_ERROR; 301b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak } 302b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak if (length == MARISA_ALPHA_ZERO_TERMINATED) { 303b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak *num_results = h->trie.predict_breadth_first( 304b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak ptr, key_ids, NULL, max_num_results); 305b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak } else { 306b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak *num_results = h->trie.predict_breadth_first( 307b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak ptr, length, key_ids, NULL, max_num_results); 308b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak } 309b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak return MARISA_ALPHA_OK; 310b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak} catch (const marisa_alpha::Exception &ex) { 311b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak return ex.status(); 312b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak} 313b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak 314b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniakmarisa_alpha_status marisa_alpha_predict_depth_first( 315b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak const marisa_alpha_trie *h, const char *ptr, size_t length, 316b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak marisa_alpha_uint32 *key_ids, size_t max_num_results, 317b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak size_t *num_results) try { 318b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak if (h == NULL) { 319b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak return MARISA_ALPHA_HANDLE_ERROR; 320b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak } else if (num_results == NULL) { 321b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak return MARISA_ALPHA_PARAM_ERROR; 322b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak } 323b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak if (length == MARISA_ALPHA_ZERO_TERMINATED) { 324b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak *num_results = h->trie.predict_depth_first( 325b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak ptr, key_ids, NULL, max_num_results); 326b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak } else { 327b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak *num_results = h->trie.predict_depth_first( 328b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak ptr, length, key_ids, NULL, max_num_results); 329b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak } 330b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak return MARISA_ALPHA_OK; 331b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak} catch (const marisa_alpha::Exception &ex) { 332b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak return ex.status(); 333b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak} 334b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak 335b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniakmarisa_alpha_status marisa_alpha_predict_callback(const marisa_alpha_trie *h, 336b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak const char *ptr, size_t length, 337b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak int (*callback)(void *, marisa_alpha_uint32, const char *, size_t), 338b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak void *first_arg_to_callback) try { 339b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak if (h == NULL) { 340b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak return MARISA_ALPHA_HANDLE_ERROR; 341b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak } else if (callback == NULL) { 342b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak return MARISA_ALPHA_PARAM_ERROR; 343b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak } 344b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak if (length == MARISA_ALPHA_ZERO_TERMINATED) { 345b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak h->trie.predict_callback(ptr, 346b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak ::PredictCallback(callback, first_arg_to_callback)); 347b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak } else { 348b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak h->trie.predict_callback(ptr, length, 349b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak ::PredictCallback(callback, first_arg_to_callback)); 350b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak } 351b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak return MARISA_ALPHA_OK; 352b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak} catch (const marisa_alpha::Exception &ex) { 353b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak return ex.status(); 354b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak} 355b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak 356b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniaksize_t marisa_alpha_get_num_tries(const marisa_alpha_trie *h) { 357b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak return (h != NULL) ? h->trie.num_tries() : 0; 358b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak} 359b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak 360b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniaksize_t marisa_alpha_get_num_keys(const marisa_alpha_trie *h) { 361b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak return (h != NULL) ? h->trie.num_keys() : 0; 362b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak} 363b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak 364b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniaksize_t marisa_alpha_get_num_nodes(const marisa_alpha_trie *h) { 365b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak return (h != NULL) ? h->trie.num_nodes() : 0; 366b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak} 367b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak 368b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniaksize_t marisa_alpha_get_total_size(const marisa_alpha_trie *h) { 369b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak return (h != NULL) ? h->trie.total_size() : 0; 370b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak} 371b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak 372b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniakmarisa_alpha_status marisa_alpha_clear(marisa_alpha_trie *h) { 373b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak if (h == NULL) { 374b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak return MARISA_ALPHA_HANDLE_ERROR; 375b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak } 376b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak h->trie.clear(); 377b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak h->mapper.clear(); 378b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak return MARISA_ALPHA_OK; 379b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak} 380b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak 381b4acd09676fe12e9d95b2a5f3f742e9a1a0483b1Przemyslaw Szczepaniak} // extern "C" 382