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

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

Issue 2559633003: Factor out bookmark-specific sorting logic from BookmarkIndex (Closed)
Patch Set: comments, virtual destructor, explicit 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') | no next file » | 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..311f13836c9253e32967967de32b26575b7f783b
--- /dev/null
+++ b/components/bookmarks/browser/typed_count_sorter.cc
@@ -0,0 +1,83 @@
+// 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 UrlTypedCountMap = BookmarkClient::UrlTypedCountMap;
+
+namespace {
+
+using UrlNodeMap = std::map<const GURL*, const TitledUrlNode*>;
+using UrlTypedCountPair = std::pair<const GURL*, int>;
+using UrlTypedCountPairs = std::vector<UrlTypedCountPair>;
+
+// Sort functor for UrlTypedCountPairs. We sort in decreasing order of typed
+// count so that the best matches will always be added to the results.
+struct UrlTypedCountPairSortFunctor {
+ bool operator()(const UrlTypedCountPair& a,
+ const UrlTypedCountPair& b) const {
+ return a.second > b.second;
+ }
+};
+
+// Extract the GURL stored in an UrlTypedCountPair and use it to look up the
+// corresponding TitledUrlNode.
+class UrlTypedCountPairNodeLookupFunctor {
+ public:
+ UrlTypedCountPairNodeLookupFunctor(UrlNodeMap& url_node_map)
+ : url_node_map_(url_node_map) {
+ }
+
+ const TitledUrlNode* operator()(const UrlTypedCountPair& pair) const {
+ return url_node_map_[pair.first];
+ }
+
+ private:
+ UrlNodeMap& url_node_map_;
+};
+
+} // 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_->SupportsTypedCountForUrls()) {
+ UrlNodeMap url_node_map;
+ UrlTypedCountMap url_typed_count_map;
+ for (auto node : matches) {
+ const GURL& url = node->GetTitledUrlNodeUrl();
+ url_node_map.insert(std::make_pair(&url, node));
+ url_typed_count_map.insert(std::make_pair(&url, 0));
+ }
+
+ client_->GetTypedCountForUrls(&url_typed_count_map);
+
+ UrlTypedCountPairs url_typed_counts;
+ std::copy(url_typed_count_map.begin(),
+ url_typed_count_map.end(),
+ std::back_inserter(url_typed_counts));
+ std::sort(url_typed_counts.begin(),
+ url_typed_counts.end(),
+ UrlTypedCountPairSortFunctor());
+ std::transform(url_typed_counts.begin(),
+ url_typed_counts.end(),
+ std::back_inserter(*sorted_nodes),
+ UrlTypedCountPairNodeLookupFunctor(url_node_map));
+ } 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') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698