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

Unified Diff: components/bookmarks/browser/typed_count_sorter.cc

Issue 2537223008: Add TitledUrlIndex for indexing arbitrary title/URL pairs (Closed)
Patch Set: add TypedCountSorter, typedef -> using Created 4 years 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 | « components/bookmarks/browser/typed_count_sorter.h ('k') | components/omnibox/browser/bookmark_provider.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: components/bookmarks/browser/typed_count_sorter.cc
diff --git a/components/bookmarks/browser/typed_count_sorter.cc b/components/bookmarks/browser/typed_count_sorter.cc
new file mode 100644
index 0000000000000000000000000000000000000000..8cb6e0a86f7c444490808b1bb0ecf85fba03acc8
--- /dev/null
+++ b/components/bookmarks/browser/typed_count_sorter.cc
@@ -0,0 +1,66 @@
+// Copyright 2016 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 "components/bookmarks/browser/typed_count_sorter.h"
+
+#include "components/bookmarks/browser/bookmark_client.h"
+
+namespace bookmarks {
+
+using BookmarkNodeSet = BookmarkClient::NodeSet;
+using NodeTypedCountPair = BookmarkClient::NodeTypedCountPair;
+using NodeTypedCountPairs = BookmarkClient::NodeTypedCountPairs;
+
+namespace {
+
+// Sort functor for NodeTypedCountPairs. We sort in decreasing order of typed
+// count so that the best matches will always be added to the results.
+struct NodeTypedCountPairSortFunctor {
+ bool operator()(const NodeTypedCountPair& a,
+ const NodeTypedCountPair& b) const {
+ return a.second > b.second;
+ }
+};
+
+// Extract the const TitledUrlNode* stored in a NodeTypedCountPair.
+struct NodeTypedCountPairExtractNodeFunctor {
+ const TitledUrlNode* operator()(const NodeTypedCountPair& pair) const {
+ return pair.first;
+ }
+};
+
+} // namespace
+
+TypedCountSorter::TypedCountSorter(BookmarkClient* client)\
+ : client_(client) {
+ DCHECK(client_);
+}
+
+TypedCountSorter::~TypedCountSorter() {}
+
+void TypedCountSorter::SortMatches(const TitledUrlNodeSet& matches,
+ TitledUrlNodes* sorted_nodes) const {
+ sorted_nodes->reserve(matches.size());
+ if (client_->SupportsTypedCountForNodes()) {
+ BookmarkNodeSet bookmark_matches;
+ for (auto titled_url_node : matches) {
+ // Ugly cast!
+ bookmark_matches.insert((BookmarkNode*)titled_url_node);
mattreynolds 2016/12/02 21:02:06 Any suggestions for getting rid of the cast?
+ }
+
+ NodeTypedCountPairs node_typed_counts;
+ client_->GetTypedCountForNodes(bookmark_matches, &node_typed_counts);
+ std::sort(node_typed_counts.begin(),
+ node_typed_counts.end(),
+ NodeTypedCountPairSortFunctor());
+ std::transform(node_typed_counts.begin(),
+ node_typed_counts.end(),
+ std::back_inserter(*sorted_nodes),
+ NodeTypedCountPairExtractNodeFunctor());
+ } else {
+ sorted_nodes->insert(sorted_nodes->end(), matches.begin(), matches.end());
+ }
+}
+
+} // namespace bookmarks
« no previous file with comments | « components/bookmarks/browser/typed_count_sorter.h ('k') | components/omnibox/browser/bookmark_provider.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698