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