7 #ifndef _FCITX_LIBIME_CORE_DATRIE_H_ 8 #define _FCITX_LIBIME_CORE_DATRIE_H_ 13 #include <libime/core/libimecore_export.h> 22 #include <string_view> 25 #include <fcitx-utils/macros.h> 29 template <
typename V,
bool ORDERED = true,
int MAX_TRIAL = 1>
34 static constexpr
auto N1 = -1;
35 static constexpr
auto N2 = -2;
39 static constexpr
auto N1 = 0x7fc00001;
40 static constexpr
auto N2 = 0x7fc00002;
58 using position_type = uint64_t;
60 std::function<bool(value_type, size_t, position_type)>;
61 using updater_type = std::function<value_type(value_type)>;
72 DATrie(
const char *filename);
75 FCITX_DECLARE_VIRTUAL_DTOR_COPY_AND_MOVE(
DATrie)
77 void load(std::istream &in);
78 void save(
const char *filename);
79 void save(std::ostream &stream);
85 void suffix(std::string &s,
size_t len, position_type pos)
const;
88 value_type exactMatchSearch(
const char *key,
size_t len)
const;
89 value_type exactMatchSearch(std::string_view key)
const {
90 return exactMatchSearch(key.data(), key.size());
93 int32_t exactMatchSearchRaw(
const char *key,
size_t len)
const;
94 int32_t exactMatchSearchRaw(std::string_view key)
const {
95 return exactMatchSearchRaw(key.data(), key.size());
98 bool hasExactMatch(std::string_view key)
const;
100 DATrie<T>::value_type traverse(std::string_view key,
101 position_type &from)
const {
102 return traverse(key.data(), key.size(), from);
104 DATrie<T>::value_type traverse(
const char *key,
size_t len,
105 position_type &from)
const;
107 int32_t traverseRaw(std::string_view key, position_type &from)
const {
108 return traverseRaw(key.data(), key.size(), from);
110 int32_t traverseRaw(
const char *key,
size_t len, position_type &from)
const;
113 void set(std::string_view key, value_type val) {
114 return set(key.data(), key.size(), val);
116 void set(
const char *key,
size_t len, value_type val);
118 void update(std::string_view key, updater_type updater) {
119 update(key.data(), key.size(), updater);
121 void update(
const char *key,
size_t len, updater_type updater);
123 void dump(value_type *data, std::size_t size)
const;
124 void dump(std::vector<value_type> &data)
const;
126 std::vector<std::tuple<value_type, size_t, position_type>> &data)
const;
129 bool erase(std::string_view key, position_type from = 0) {
130 return erase(key.data(), key.size(), from);
132 bool erase(
const char *key,
size_t len, position_type from = 0);
133 bool erase(position_type from = 0);
136 bool foreach(callback_type func, position_type pos = 0)
const;
138 bool foreach(
const char *prefix,
size_t size, callback_type func,
139 position_type pos = 0)
const;
140 bool foreach(std::string_view prefix, callback_type func,
141 position_type pos = 0)
const {
142 return foreach(prefix.data(), prefix.size(), func, pos);
147 static bool isValid(value_type v);
148 static bool isNoPath(value_type v);
149 static bool isNoValue(value_type v);
151 static bool isValidRaw(int32_t v);
152 static bool isNoPathRaw(int32_t v);
153 static bool isNoValueRaw(int32_t v);
155 static value_type noPath();
156 static value_type noValue();
158 static value_type decode(int32_t raw);
160 size_t mem_size()
const;
163 std::unique_ptr<DATriePrivate<value_type>> d;
171 #endif // _FCITX_LIBIME_CORE_DATRIE_H_
This is a trie based on cedar<www.tkl.iis.u-tokyo.ac.jp/~ynaga/cedar/>.