| 1 | // Copyright 2013 The Flutter Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef FLUTTER_FML_ASCIITRIE_H_ |
| 6 | #define FLUTTER_FML_ASCIITRIE_H_ |
| 7 | |
| 8 | #include <memory> |
| 9 | #include <string> |
| 10 | #include <vector> |
| 11 | |
| 12 | namespace fml { |
| 13 | |
| 14 | /// A trie for looking for ASCII prefixes. |
| 15 | class AsciiTrie { |
| 16 | public: |
| 17 | struct TrieNode; |
| 18 | typedef std::unique_ptr<TrieNode> TrieNodePtr; |
| 19 | /// The max Ascii value. |
| 20 | static const int kMaxAsciiValue = 128; |
| 21 | |
| 22 | /// Clear and insert all the entries into the trie. |
| 23 | void Fill(const std::vector<std::string>& entries); |
| 24 | |
| 25 | /// Returns true if \p argument is prefixed by the contents of the trie. |
| 26 | inline bool Query(const char* argument) { |
| 27 | return !node_ || Query(trie: node_.get(), query: argument); |
| 28 | } |
| 29 | |
| 30 | struct TrieNode { |
| 31 | TrieNodePtr children[kMaxAsciiValue]; |
| 32 | }; |
| 33 | |
| 34 | private: |
| 35 | static bool Query(TrieNode* trie, const char* query); |
| 36 | TrieNodePtr node_; |
| 37 | }; |
| 38 | } // namespace fml |
| 39 | |
| 40 | #endif |
| 41 | |