Chromium Code Reviews| 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 |