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 | |
18 #include <libaddressinput/util/basictypes.h> | |
19 | 7 |
20 #include <list> | 8 #include <list> |
21 #include <map> | 9 #include <map> |
22 #include <set> | 10 #include <set> |
23 #include <string> | 11 #include <string> |
24 | 12 |
25 namespace i18n { | 13 #include "base/basictypes.h" |
26 namespace addressinput { | 14 |
| 15 namespace autofill { |
27 | 16 |
28 // A prefix search tree. Can return all objects whose keys start with a prefix | 17 // A prefix search tree. Can return all objects whose keys start with a prefix |
29 // string. | 18 // string. |
30 // | 19 // |
31 // Maps keys to multiple objects. This property is useful when mapping region | 20 // 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, | 21 // 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 | 22 // and there's a "St. Petersburg" in Russia. A lookup for "St. Petersburg" key |
34 // should return two distinct objects. | 23 // should return two distinct objects. |
35 template <typename T> | 24 template <typename T> |
36 class Trie { | 25 class Trie { |
37 public: | 26 public: |
38 Trie(); | 27 Trie(); |
39 | |
40 ~Trie(); | 28 ~Trie(); |
41 | 29 |
42 // Adds a mapping from |key| to |data_item|. Can be called with the same |key| | 30 // Adds a mapping from |key| to |data_item|. Can be called with the same |key| |
43 // multiple times. | 31 // multiple times. |
44 void AddDataForKey(const std::string& key, const T& data_item); | 32 void AddDataForKey(const std::string& key, const T& data_item); |
45 | 33 |
46 // Adds all objects whose keys start with |key_prefix| to the |results| | 34 // Adds all objects whose keys start with |key_prefix| to the |results| |
47 // parameter. The |results| parameter should not be NULL. | 35 // parameter. The |results| parameter should not be NULL. |
48 void FindDataForKeyPrefix(const std::string& key_prefix, | 36 void FindDataForKeyPrefix(const std::string& key_prefix, |
49 std::set<T>* results) const; | 37 std::set<T>* results) const; |
50 | 38 |
51 private: | 39 private: |
52 // All objects for this node in the trie. This field is a collection to enable | 40 // All objects for this node in the trie. This field is a collection to enable |
53 // mapping the same key to multiple objects. | 41 // mapping the same key to multiple objects. |
54 std::list<T> data_list_; | 42 std::list<T> data_list_; |
55 | 43 |
56 // Trie sub nodes. The root trie stores the objects for the empty string key. | 44 // 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. | 45 // 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 | 46 // The grand-children of the root trie store the objects for the two-letter |
59 // keys, and so on. | 47 // keys, and so on. |
60 std::map<char, Trie<T>*> sub_nodes_; | 48 std::map<char, Trie<T>*> sub_nodes_; |
61 | 49 |
62 DISALLOW_COPY_AND_ASSIGN(Trie); | 50 DISALLOW_COPY_AND_ASSIGN(Trie); |
63 }; | 51 }; |
64 | 52 |
65 } // namespace addressinput | 53 } // namespace autofill |
66 } // namespace i18n | |
67 | 54 |
68 #endif // I18N_ADDRESSINPUT_UTIL_TRIE_H_ | 55 #endif // THIRD_PARTY_LIBADDRESSINPUT_CHROMIUM_TRIE_H_ |
OLD | NEW |