Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(209)

Side by Side Diff: third_party/libaddressinput/chromium/trie.cc

Issue 298863012: Use upstream libaddressinput in Chrome. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Self review. Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 #include "trie.h" 5 #include "third_party/libaddressinput/chromium/trie.h"
16 6
17 #include <cassert>
18 #include <cstddef> 7 #include <cstddef>
19 #include <map> 8 #include <map>
20 #include <queue> 9 #include <queue>
21 #include <set> 10 #include <set>
22 #include <string> 11 #include <string>
23 #include <utility> 12 #include <utility>
24 13
25 #include "stl_util.h" 14 #include "base/logging.h"
15 #include "base/stl_util.h"
26 16
17 // Separating template definitions and declarations requires defining all
18 // possible template parameters to avoid linking errors.
27 namespace i18n { 19 namespace i18n {
28 namespace addressinput { 20 namespace addressinput {
21 class RegionData;
22 } // namespace addressinput
23 } // namespace i18n
please use gerrit instead 2014/06/05 22:22:49 No need for closing namespace comment on a forward
please use gerrit instead 2014/06/09 23:28:17 Done.
24
25 namespace autofill {
29 26
30 template <typename T> 27 template <typename T>
31 Trie<T>::Trie() {} 28 Trie<T>::Trie() {}
32 29
33 template <typename T> 30 template <typename T>
34 Trie<T>::~Trie() { 31 Trie<T>::~Trie() {
35 STLDeleteValues(&sub_nodes_); 32 STLDeleteValues(&sub_nodes_);
36 } 33 }
37 34
38 template <typename T> 35 template <typename T>
39 void Trie<T>::AddDataForKey(const std::string& key, const T& data_item) { 36 void Trie<T>::AddDataForKey(const std::string& key, const T& data_item) {
40 Trie<T>* current_node = this; 37 Trie<T>* current_node = this;
41 for (size_t key_start = 0; key_start < key.length(); ++key_start) { 38 for (size_t key_start = 0; key_start < key.length(); ++key_start) {
42 typename std::map<char, Trie<T>*>::const_iterator sub_node_it = 39 typename std::map<char, Trie<T>*>::const_iterator sub_node_it =
43 current_node->sub_nodes_.find(key[key_start]); 40 current_node->sub_nodes_.find(key[key_start]);
44 if (sub_node_it == current_node->sub_nodes_.end()) { 41 if (sub_node_it == current_node->sub_nodes_.end()) {
45 sub_node_it = current_node->sub_nodes_.insert( 42 sub_node_it = current_node->sub_nodes_.insert(
46 std::make_pair(key[key_start], new Trie<T>)).first; 43 std::make_pair(key[key_start], new Trie<T>)).first;
47 } 44 }
48 current_node = sub_node_it->second; 45 current_node = sub_node_it->second;
49 assert(current_node != NULL); 46 DCHECK(current_node);
50 } 47 }
51 current_node->data_list_.push_back(data_item); 48 current_node->data_list_.push_back(data_item);
52 } 49 }
53 50
54 template <typename T> 51 template <typename T>
55 void Trie<T>::FindDataForKeyPrefix(const std::string& key_prefix, 52 void Trie<T>::FindDataForKeyPrefix(const std::string& key_prefix,
56 std::set<T>* results) const { 53 std::set<T>* results) const {
57 assert(results != NULL); 54 DCHECK(results);
58 55
59 // Find the sub-trie for the key prefix. 56 // Find the sub-trie for the key prefix.
60 const Trie<T>* current_node = this; 57 const Trie<T>* current_node = this;
61 for (size_t key_prefix_start = 0; key_prefix_start < key_prefix.length(); 58 for (size_t key_prefix_start = 0; key_prefix_start < key_prefix.length();
62 ++key_prefix_start) { 59 ++key_prefix_start) {
63 typename std::map<char, Trie<T>*>::const_iterator sub_node_it = 60 typename std::map<char, Trie<T>*>::const_iterator sub_node_it =
64 current_node->sub_nodes_.find(key_prefix[key_prefix_start]); 61 current_node->sub_nodes_.find(key_prefix[key_prefix_start]);
65 if (sub_node_it == current_node->sub_nodes_.end()) { 62 if (sub_node_it == current_node->sub_nodes_.end()) {
66 return; 63 return;
67 } 64 }
68 current_node = sub_node_it->second; 65 current_node = sub_node_it->second;
69 assert(current_node != NULL); 66 DCHECK(current_node);
70 } 67 }
71 68
72 // Collect data from all sub-tries. 69 // Collect data from all sub-tries.
73 std::queue<const Trie<T>*> node_queue; 70 std::queue<const Trie<T>*> node_queue;
74 node_queue.push(current_node); 71 node_queue.push(current_node);
75 while (!node_queue.empty()) { 72 while (!node_queue.empty()) {
76 const Trie<T>* queue_front = node_queue.front(); 73 const Trie<T>* queue_front = node_queue.front();
77 node_queue.pop(); 74 node_queue.pop();
78 75
79 results->insert( 76 results->insert(
80 queue_front->data_list_.begin(), queue_front->data_list_.end()); 77 queue_front->data_list_.begin(), queue_front->data_list_.end());
81 78
82 for (typename std::map<char, Trie<T>*>::const_iterator sub_node_it = 79 for (typename std::map<char, Trie<T>*>::const_iterator sub_node_it =
83 queue_front->sub_nodes_.begin(); 80 queue_front->sub_nodes_.begin();
84 sub_node_it != queue_front->sub_nodes_.end(); 81 sub_node_it != queue_front->sub_nodes_.end();
85 ++sub_node_it) { 82 ++sub_node_it) {
86 node_queue.push(sub_node_it->second); 83 node_queue.push(sub_node_it->second);
87 } 84 }
88 } 85 }
89 } 86 }
90 87
91 // Separating template definitions and declarations requires defining all 88 template class Trie<const ::i18n::addressinput::RegionData*>;
92 // possible template parameters to avoid linking errors.
93 class Ruleset;
94 template class Trie<const Ruleset*>;
95 template class Trie<std::string>; 89 template class Trie<std::string>;
96 90
97 } // namespace addressinput 91 } // namespace autofill
98 } // namespace i18n
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698