OLD | NEW |
1 // Copyright (C) 2014 Google Inc. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // found in the LICENSE file. |
4 // you may not use this file except in compliance with the License. | |
5 // You may obtain a copy of the License at | |
6 // | |
7 // http://www.apache.org/licenses/LICENSE-2.0 | |
8 // | |
9 // Unless required by applicable law or agreed to in writing, software | |
10 // distributed under the License is distributed on an "AS IS" BASIS, | |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 // See the License for the specific language governing permissions and | |
13 // limitations under the License. | |
14 | 4 |
15 #ifndef I18N_ADDRESSINPUT_UTIL_TRIE_H_ | 5 #ifndef THIRD_PARTY_LIBADDRESSINPUT_CHROMIUM_TRIE_H_ |
16 #define I18N_ADDRESSINPUT_UTIL_TRIE_H_ | 6 #define THIRD_PARTY_LIBADDRESSINPUT_CHROMIUM_TRIE_H_ |
17 | 7 |
18 #include <libaddressinput/util/basictypes.h> | |
19 | |
20 #include <list> | |
21 #include <map> | 8 #include <map> |
22 #include <set> | 9 #include <set> |
23 #include <string> | 10 #include <string> |
24 | 11 |
25 namespace i18n { | 12 #include "base/basictypes.h" |
26 namespace addressinput { | 13 |
| 14 namespace autofill { |
27 | 15 |
28 // A prefix search tree. Can return all objects whose keys start with a prefix | 16 // A prefix search tree. Can return all objects whose keys start with a prefix |
29 // string. | 17 // string. |
30 // | 18 // |
31 // Maps keys to multiple objects. This property is useful when mapping region | 19 // Maps keys to multiple objects. This property is useful when mapping region |
32 // names to region objects. For example, there's a "St. Petersburg" in Florida, | 20 // names to region objects. For example, there's a "St. Petersburg" in Florida, |
33 // and there's a "St. Petersburg" in Russia. A lookup for "St. Petersburg" key | 21 // and there's a "St. Petersburg" in Russia. A lookup for "St. Petersburg" key |
34 // should return two distinct objects. | 22 // should return two distinct objects. |
35 template <typename T> | 23 template <typename T> |
36 class Trie { | 24 class Trie { |
37 public: | 25 public: |
38 Trie(); | 26 Trie(); |
39 | |
40 ~Trie(); | 27 ~Trie(); |
41 | 28 |
| 29 // Returns true if no data was added in AddDataForKey(). |
| 30 bool empty() const { return data_list_.empty() && sub_nodes_.empty(); } |
| 31 |
42 // Adds a mapping from |key| to |data_item|. Can be called with the same |key| | 32 // Adds a mapping from |key| to |data_item|. Can be called with the same |key| |
43 // multiple times. | 33 // multiple times. Does not take ownership of |key|, which should not be NULL. |
44 void AddDataForKey(const std::string& key, const T& data_item); | 34 void AddDataForKey(const uint8_t* key, int32_t key_size, const T& data_item); |
45 | 35 |
46 // Adds all objects whose keys start with |key_prefix| to the |results| | 36 // Adds all objects whose keys start with |key_prefix| to the |results| |
47 // parameter. The |results| parameter should not be NULL. | 37 // parameter. Does not take ownership of |key_prefix|, which should not be |
48 void FindDataForKeyPrefix(const std::string& key_prefix, | 38 // NULL. The |results| parameter should not be NULL. |
| 39 void FindDataForKeyPrefix(const uint8_t* key_prefix, |
| 40 int32_t key_prefix_size, |
49 std::set<T>* results) const; | 41 std::set<T>* results) const; |
50 | 42 |
51 private: | 43 private: |
52 // All objects for this node in the trie. This field is a collection to enable | 44 // All objects for this node in the trie. This field is a collection to enable |
53 // mapping the same key to multiple objects. | 45 // mapping the same key to multiple objects. |
54 std::list<T> data_list_; | 46 std::set<T> data_list_; |
55 | 47 |
56 // Trie sub nodes. The root trie stores the objects for the empty string key. | 48 // Trie sub nodes. The root trie stores the objects for the empty string key. |
57 // The children of the root trie store the objects for the one-letter keys. | 49 // The children of the root trie store the objects for the one-letter keys. |
58 // The grand-children of the root trie store the objects for the two-letter | 50 // The grand-children of the root trie store the objects for the two-letter |
59 // keys, and so on. | 51 // keys, and so on. |
60 std::map<char, Trie<T>*> sub_nodes_; | 52 std::map<uint8_t, Trie<T>*> sub_nodes_; |
61 | 53 |
62 DISALLOW_COPY_AND_ASSIGN(Trie); | 54 DISALLOW_COPY_AND_ASSIGN(Trie); |
63 }; | 55 }; |
64 | 56 |
65 } // namespace addressinput | 57 } // namespace autofill |
66 } // namespace i18n | |
67 | 58 |
68 #endif // I18N_ADDRESSINPUT_UTIL_TRIE_H_ | 59 #endif // THIRD_PARTY_LIBADDRESSINPUT_CHROMIUM_TRIE_H_ |
OLD | NEW |