1 #ifndef MARISA_SWIG_H_ 2 #define MARISA_SWIG_H_ 3 4 #include <marisa.h> 5 6 namespace marisa_swig { 7 8 #define MARISA_SWIG_ENUM_COPY(name) name = MARISA_ ## name 9 10 enum ErrorCode { 11 MARISA_SWIG_ENUM_COPY(OK), 12 MARISA_SWIG_ENUM_COPY(STATE_ERROR), 13 MARISA_SWIG_ENUM_COPY(NULL_ERROR), 14 MARISA_SWIG_ENUM_COPY(BOUND_ERROR), 15 MARISA_SWIG_ENUM_COPY(RANGE_ERROR), 16 MARISA_SWIG_ENUM_COPY(CODE_ERROR), 17 MARISA_SWIG_ENUM_COPY(RESET_ERROR), 18 MARISA_SWIG_ENUM_COPY(SIZE_ERROR), 19 MARISA_SWIG_ENUM_COPY(MEMORY_ERROR), 20 MARISA_SWIG_ENUM_COPY(IO_ERROR), 21 MARISA_SWIG_ENUM_COPY(FORMAT_ERROR) 22 }; 23 24 enum NumTries { 25 MARISA_SWIG_ENUM_COPY(MIN_NUM_TRIES), 26 MARISA_SWIG_ENUM_COPY(MAX_NUM_TRIES), 27 MARISA_SWIG_ENUM_COPY(DEFAULT_NUM_TRIES) 28 }; 29 30 enum CacheLevel { 31 MARISA_SWIG_ENUM_COPY(HUGE_CACHE), 32 MARISA_SWIG_ENUM_COPY(LARGE_CACHE), 33 MARISA_SWIG_ENUM_COPY(NORMAL_CACHE), 34 MARISA_SWIG_ENUM_COPY(SMALL_CACHE), 35 MARISA_SWIG_ENUM_COPY(TINY_CACHE), 36 MARISA_SWIG_ENUM_COPY(DEFAULT_CACHE) 37 }; 38 39 enum TailMode { 40 MARISA_SWIG_ENUM_COPY(TEXT_TAIL), 41 MARISA_SWIG_ENUM_COPY(BINARY_TAIL), 42 MARISA_SWIG_ENUM_COPY(DEFAULT_TAIL) 43 }; 44 45 enum NodeOrder { 46 MARISA_SWIG_ENUM_COPY(LABEL_ORDER), 47 MARISA_SWIG_ENUM_COPY(WEIGHT_ORDER), 48 MARISA_SWIG_ENUM_COPY(DEFAULT_ORDER) 49 }; 50 51 #undef MARISA_SWIG_ENUM_COPY 52 53 class Key { 54 public: 55 void str(const char **ptr_out, std::size_t *length_out) const; 56 std::size_t id() const; 57 float weight() const; 58 59 private: 60 const marisa::Key key_; 61 62 Key(); 63 Key(const Key &key); 64 Key &operator=(const Key &); 65 }; 66 67 class Query { 68 public: 69 void str(const char **ptr_out, std::size_t *length_out) const; 70 std::size_t id() const; 71 72 private: 73 const marisa::Query query_; 74 75 Query(); 76 Query(const Query &query); 77 Query &operator=(const Query &); 78 }; 79 80 class Keyset { 81 friend class Trie; 82 83 public: 84 Keyset(); 85 ~Keyset(); 86 87 void push_back(const marisa::Key &key); 88 void push_back(const char *ptr, std::size_t length, float weight = 1.0); 89 90 const Key &key(std::size_t i) const; 91 92 void key_str(std::size_t i, 93 const char **ptr_out, std::size_t *length_out) const; 94 std::size_t key_id(std::size_t i) const; 95 96 std::size_t num_keys() const; 97 98 bool empty() const; 99 std::size_t size() const; 100 std::size_t total_length() const; 101 102 void reset(); 103 void clear(); 104 105 private: 106 marisa::Keyset *keyset_; 107 108 Keyset(const Keyset &); 109 Keyset &operator=(const Keyset &); 110 }; 111 112 class Agent { 113 friend class Trie; 114 115 public: 116 Agent(); 117 ~Agent(); 118 119 void set_query(const char *ptr, std::size_t length); 120 void set_query(std::size_t id); 121 122 const Key &key() const; 123 const Query &query() const; 124 125 void key_str(const char **ptr_out, std::size_t *length_out) const; 126 std::size_t key_id() const; 127 128 void query_str(const char **ptr_out, std::size_t *length_out) const; 129 std::size_t query_id() const; 130 131 private: 132 marisa::Agent *agent_; 133 char *buf_; 134 std::size_t buf_size_; 135 136 Agent(const Agent &); 137 Agent &operator=(const Agent &); 138 }; 139 140 class Trie { 141 public: 142 Trie(); 143 ~Trie(); 144 145 void build(Keyset &keyset, int config_flags = 0); 146 147 void mmap(const char *filename); 148 void load(const char *filename); 149 void save(const char *filename) const; 150 151 bool lookup(Agent &agent) const; 152 void reverse_lookup(Agent &agent) const; 153 bool common_prefix_search(Agent &agent) const; 154 bool predictive_search(Agent &agent) const; 155 156 std::size_t lookup(const char *ptr, std::size_t length) const; 157 void reverse_lookup(std::size_t id, 158 const char **ptr_out_to_be_deleted, std::size_t *length_out) const; 159 160 std::size_t num_tries() const; 161 std::size_t num_keys() const; 162 std::size_t num_nodes() const; 163 164 TailMode tail_mode() const; 165 NodeOrder node_order() const; 166 167 bool empty() const; 168 std::size_t size() const; 169 std::size_t total_size() const; 170 std::size_t io_size() const; 171 172 void clear(); 173 174 private: 175 marisa::Trie *trie_; 176 177 Trie(const Trie &); 178 Trie &operator=(const Trie &); 179 }; 180 181 } // namespace marisa_swig 182 183 #endif // MARISA_SWIG_H_ 184