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

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: Make StringCanonicalizer not a scoped_ptr. Created 6 years, 5 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
(Empty)
1 // Copyright 2014 The Chromium 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 #include "third_party/libaddressinput/chromium/trie.h"
6
7 #include <cstddef>
8 #include <map>
9 #include <queue>
10 #include <set>
11 #include <string>
12 #include <utility>
13
14 #include "base/logging.h"
15 #include "base/stl_util.h"
16
17 // Separating template definitions and declarations requires defining all
18 // possible template parameters to avoid linking errors.
19 namespace i18n {
20 namespace addressinput {
21 class RegionData;
22 }
23 }
24
25 namespace autofill {
26
27 template <typename T>
28 Trie<T>::Trie() {
29 }
30
31 template <typename T>
32 Trie<T>::~Trie() {
33 STLDeleteValues(&sub_nodes_);
34 }
35
36 template <typename T>
37 void Trie<T>::AddDataForKey(const uint8_t* key,
38 int32_t key_size,
39 const T& data_item) {
40 Trie<T>* current_node = this;
41 for (int32_t i = 0; i < key_size; ++i) {
42 typename std::map<uint8_t, Trie<T>*>::const_iterator sub_node_it =
43 current_node->sub_nodes_.find(key[i]);
44 if (sub_node_it == current_node->sub_nodes_.end()) {
45 sub_node_it =
46 current_node->sub_nodes_.insert(std::make_pair(key[i], new Trie<T>))
47 .first;
48 }
49 current_node = sub_node_it->second;
50 DCHECK(current_node);
51 }
52 current_node->data_list_.insert(data_item);
53 }
54
55 template <typename T>
56 void Trie<T>::FindDataForKeyPrefix(const uint8_t* key_prefix,
57 int32_t key_prefix_size,
58 std::set<T>* results) const {
59 DCHECK(results);
60
61 // Find the sub-trie for the key prefix.
62 const Trie<T>* current_node = this;
63 for (int32_t i = 0; i < key_prefix_size; ++i) {
64 typename std::map<uint8_t, Trie<T>*>::const_iterator sub_node_it =
65 current_node->sub_nodes_.find(key_prefix[i]);
66 if (sub_node_it == current_node->sub_nodes_.end())
67 return;
68
69 current_node = sub_node_it->second;
70 DCHECK(current_node);
71 }
72
73 // Collect data from all sub-tries.
74 std::queue<const Trie<T>*> node_queue;
75 node_queue.push(current_node);
76 while (!node_queue.empty()) {
77 const Trie<T>* queue_front = node_queue.front();
78 node_queue.pop();
79
80 results->insert(queue_front->data_list_.begin(),
81 queue_front->data_list_.end());
82
83 for (typename std::map<uint8_t, Trie<T>*>::const_iterator sub_node_it =
84 queue_front->sub_nodes_.begin();
85 sub_node_it != queue_front->sub_nodes_.end();
86 ++sub_node_it) {
87 node_queue.push(sub_node_it->second);
88 }
89 }
90 }
91
92 template class Trie<const ::i18n::addressinput::RegionData*>;
93 template class Trie<std::string>;
94
95 } // namespace autofill
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698