1 // Copyright (C) 2014 Google Inc.
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
15 #ifndef I18N_ADDRESSINPUT_UTIL_TRIE_H_
16 #define I18N_ADDRESSINPUT_UTIL_TRIE_H_
18 #include <libaddressinput/util/basictypes.h>
26 namespace addressinput {
28 // A prefix search tree. Can return all objects whose keys start with a prefix
31 // 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,
33 // and there's a "St. Petersburg" in Russia. A lookup for "St. Petersburg" key
34 // should return two distinct objects.
42 // Adds a mapping from |key| to |data_item|. Can be called with the same |key|
44 void AddDataForKey(const std::string& key, const T& data_item);
46 // Adds all objects whose keys start with |key_prefix| to the |results|
47 // parameter. The |results| parameter should not be NULL.
48 void FindDataForKeyPrefix(const std::string& key_prefix,
49 std::set<T>* results) const;
52 // All objects for this node in the trie. This field is a collection to enable
53 // mapping the same key to multiple objects.
54 std::list<T> data_list_;
56 // 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.
58 // The grand-children of the root trie store the objects for the two-letter
60 std::map<char, Trie<T>*> sub_nodes_;
62 DISALLOW_COPY_AND_ASSIGN(Trie);
65 } // namespace addressinput
68 #endif // I18N_ADDRESSINPUT_UTIL_TRIE_H_