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

Unified Diff: third_party/libaddressinput/chromium/trie.cc

Issue 389733002: Reland "Use upstream libaddressinput in Chrome." (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix GN build. 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « third_party/libaddressinput/chromium/trie.h ('k') | third_party/libaddressinput/chromium/trie_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: third_party/libaddressinput/chromium/trie.cc
diff --git a/third_party/libaddressinput/chromium/trie.cc b/third_party/libaddressinput/chromium/trie.cc
new file mode 100644
index 0000000000000000000000000000000000000000..e9289af06ee4952fd1aef853c5d0467257b7cc97
--- /dev/null
+++ b/third_party/libaddressinput/chromium/trie.cc
@@ -0,0 +1,81 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "third_party/libaddressinput/chromium/trie.h"
+
+#include <queue>
+#include <string>
+
+#include "base/logging.h"
+
+// Separating template definitions and declarations requires defining all
+// possible template parameters to avoid linking errors.
+namespace i18n {
+namespace addressinput {
+class RegionData;
+}
+}
+
+namespace autofill {
+
+template <typename T>
+Trie<T>::Trie() {}
+
+template <typename T>
+Trie<T>::~Trie() {}
+
+template <typename T>
+void Trie<T>::AddDataForKey(const std::vector<uint8_t>& key,
+ const T& data_item) {
+ Trie<T>* current_node = this;
+ for (std::vector<uint8_t>::size_type i = 0; i < key.size(); ++i) {
+ if (!key[i])
+ break;
+ current_node = &current_node->sub_nodes_[key[i]];
+ }
+ current_node->data_list_.insert(data_item);
+}
+
+template <typename T>
+void Trie<T>::FindDataForKeyPrefix(const std::vector<uint8_t>& key_prefix,
+ std::set<T>* results) const {
+ DCHECK(results);
+
+ // Find the sub-trie for the key prefix.
+ const Trie<T>* current_node = this;
+ for (std::vector<uint8_t>::size_type i = 0; i < key_prefix.size(); ++i) {
+ if (!key_prefix[i])
+ break;
+
+ typename std::map<uint8_t, Trie<T> >::const_iterator sub_node_it =
+ current_node->sub_nodes_.find(key_prefix[i]);
+ if (sub_node_it == current_node->sub_nodes_.end())
+ return;
+
+ current_node = &sub_node_it->second;
+ }
+
+ // Collect data from all sub-tries.
+ std::queue<const Trie<T>*> node_queue;
+ node_queue.push(current_node);
+ while (!node_queue.empty()) {
+ const Trie<T>* queue_front = node_queue.front();
+ node_queue.pop();
+
+ results->insert(queue_front->data_list_.begin(),
+ queue_front->data_list_.end());
+
+ for (typename std::map<uint8_t, Trie<T> >::const_iterator sub_node_it =
+ queue_front->sub_nodes_.begin();
+ sub_node_it != queue_front->sub_nodes_.end();
+ ++sub_node_it) {
+ node_queue.push(&sub_node_it->second);
+ }
+ }
+}
+
+template class Trie<const ::i18n::addressinput::RegionData*>;
+template class Trie<std::string>;
+
+} // namespace autofill
« no previous file with comments | « third_party/libaddressinput/chromium/trie.h ('k') | third_party/libaddressinput/chromium/trie_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698