| 26 | // |
| 27 | |
| 28 | struct naive_trie { |
| 29 | naive_trie() : has_value(false), value(0) { |
| 30 | } |
| 31 | void insert(const char * key, size_t len, int32_t value = 0) { |
| 32 | if (len == 0) { |
| 33 | this->has_value = true; |
| 34 | this->value = value; |
| 35 | return; |
| 36 | } |
| 37 | char c = key[0]; |
| 38 | auto res = children.find(c); |
| 39 | if (res != children.end()) { |
| 40 | res->second.insert(key + 1, len - 1, value); |
| 41 | } else { |
| 42 | auto res = children.insert(std::make_pair(c, naive_trie())); |
| 43 | res.first->second.insert(key + 1, len - 1, value); |
| 44 | } |
| 45 | } |
| 46 | std::pair<const char *, size_t> get_longest_prefix(const char * key, size_t len, size_t offset = 0) const { |
| 47 | if (len == 0 || offset == len) { |
| 48 | return std::make_pair(key, offset); |
| 49 | } |
| 50 | char c = key[offset]; |
| 51 | auto res = children.find(c); |
| 52 | if (res != children.end()) { |
| 53 | return res->second.get_longest_prefix(key, len, offset + 1); |
| 54 | } |
| 55 | |
| 56 | return std::make_pair(key, offset); |
| 57 | } |
| 58 | const struct naive_trie * traverse(const char c) const { |
| 59 | auto res = children.find(c); |
| 60 | if (res != children.end()) { |
| 61 | return &res->second; |
| 62 | } |
| 63 | |
| 64 | return NULL; |
| 65 | } |
| 66 | std::map<char, struct naive_trie> children; |
| 67 | bool has_value; |
| 68 | llama_token value; |
| 69 | }; |
| 70 | |
| 71 | // |
| 72 | // tokenizers |