OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/app_list/search/mixer.h" | 5 #include "chrome/browser/ui/app_list/search/mixer.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <set> | 8 #include <set> |
9 #include <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
59 if (id_set.find(id) != id_set.end()) | 59 if (id_set.find(id) != id_set.end()) |
60 continue; | 60 continue; |
61 | 61 |
62 id_set.insert(id); | 62 id_set.insert(id); |
63 final.push_back(*it); | 63 final.push_back(*it); |
64 } | 64 } |
65 | 65 |
66 results->swap(final); | 66 results->swap(final); |
67 } | 67 } |
68 | 68 |
69 // Publishes the given |results| to |ui_results|. Reuse existing ones to avoid | 69 // Publishes the given |new_results| to |ui_results|. Reuse existing ones to |
Matt Giuca
2014/07/04 08:21:21
I'm a little skeptical of the name |new_results|,
calamity
2014/07/08 09:47:20
Done. Also changed the second sentence to better d
| |
70 // flickering. | 70 // avoid flickering. |
71 void Publish(const SortedResults& results, | 71 void Publish(const SortedResults& new_results, |
Matt Giuca
2014/07/04 08:21:21
I think there should be unit tests for this functi
calamity
2014/07/08 09:47:21
Done.
| |
72 AppListModel::SearchResults* ui_results) { | 72 AppListModel::SearchResults* ui_results) { |
Matt Giuca
2014/07/04 08:21:22
It might be nice to give a high-level overview of
calamity
2014/07/08 09:47:20
Done. Changed for new algorithm.
| |
73 for (size_t i = 0; i < results.size(); ++i) { | 73 typedef std::map<std::string, ChromeSearchResult*> IdToResultMap; |
74 ChromeSearchResult* result = results[i].result; | |
75 | 74 |
76 ChromeSearchResult* ui_result = i < ui_results->item_count() ? | 75 IdToResultMap new_results_map; |
77 static_cast<ChromeSearchResult*>(ui_results->GetItemAt(i)) : NULL; | 76 for (size_t i = 0; i < new_results.size(); ++i) |
Matt Giuca
2014/07/04 08:21:21
Use an iterator.
calamity
2014/07/08 09:47:21
Done.
| |
78 if (ui_result && ui_result->id() == result->id()) { | 77 new_results_map[new_results[i].result->id()] = new_results[i].result; |
79 ui_result->set_title(result->title()); | 78 |
80 ui_result->set_title_tags(result->title_tags()); | 79 // A map of the items in |ui_results| that takes ownership of the items. |
81 ui_result->set_details(result->details()); | 80 IdToResultMap ui_results_map; |
82 ui_result->set_details_tags(result->details_tags()); | 81 for (size_t i = 0; i < ui_results->item_count(); ++i) { |
83 ui_results->NotifyItemsChanged(i, 1); | 82 ChromeSearchResult* ui_result = |
83 static_cast<ChromeSearchResult*>(ui_results->GetItemAt(i)); | |
84 ui_results_map[ui_result->id()] = ui_result; | |
85 } | |
86 // We have to erase all results at once so that observers are notified with | |
87 // meaningful indexes. | |
Matt Giuca
2014/07/04 08:21:22
Does this mean observers will see a "remove" of a
calamity
2014/07/08 09:47:21
Yes. The alternative is to use a Move(), but the o
| |
88 ui_results->RemoveAll(); | |
89 | |
90 // For each ui result, update its properties if it is in the new results, | |
91 // otherwise delete it. | |
92 std::vector<std::string> ids_to_delete; | |
93 for (IdToResultMap::const_iterator ui_result_it = ui_results_map.begin(); | |
94 ui_result_it != ui_results_map.end(); | |
95 ++ui_result_it) { | |
96 ChromeSearchResult* ui_result = ui_result_it->second; | |
97 IdToResultMap::const_iterator result_it = | |
98 new_results_map.find(ui_result->id()); | |
99 if (result_it != new_results_map.end()) { | |
100 ChromeSearchResult* new_result = result_it->second; | |
Matt Giuca
2014/07/04 08:21:22
Can I suggest (the original code could have used t
calamity
2014/07/08 09:47:21
Done.
| |
101 ui_result->set_title(new_result->title()); | |
102 ui_result->set_title_tags(new_result->title_tags()); | |
103 ui_result->set_details(new_result->details()); | |
104 ui_result->set_details_tags(new_result->details_tags()); | |
105 new_results_map.erase(result_it); | |
Matt Giuca
2014/07/04 08:21:21
Is there a reason you need to erase results from n
| |
84 } else { | 106 } else { |
85 if (ui_result) | 107 ids_to_delete.push_back(ui_result->id()); |
86 ui_results->DeleteAt(i); | |
87 ui_results->AddAt(i, result->Duplicate().release()); | |
88 } | 108 } |
89 } | 109 } |
90 | 110 |
91 while (ui_results->item_count() > results.size()) | 111 for (size_t i = 0; i < ids_to_delete.size(); ++i) { |
92 ui_results->DeleteAt(ui_results->item_count() - 1); | 112 delete ui_results_map[ids_to_delete[i]]; |
113 ui_results_map.erase(ids_to_delete[i]); | |
114 } | |
115 | |
116 // Insert results in the order of |new_results|. | |
Matt Giuca
2014/07/04 08:21:21
Before this comment, say:
// Now, ui_results_map c
calamity
2014/07/08 09:47:21
Done.
| |
117 for (size_t i = 0; i < new_results.size(); ++i) { | |
118 ChromeSearchResult* new_result = new_results[i].result; | |
119 IdToResultMap::const_iterator ui_result_it = | |
120 ui_results_map.find(new_result->id()); | |
121 // |ui_results| takes ownership here. | |
122 ui_results->Add(ui_result_it != ui_results_map.end() | |
123 ? ui_result_it->second | |
124 : new_result->Duplicate().release()); | |
Matt Giuca
2014/07/04 08:21:21
Should you be calling NotifyItemsChanged here? The
calamity
2014/07/08 09:47:21
Not necessary as the add and remove are used to no
| |
125 } | |
93 } | 126 } |
94 | 127 |
95 } // namespace | 128 } // namespace |
96 | 129 |
97 // Used to group relevant providers together fox mixing their results. | 130 // Used to group relevant providers together fox mixing their results. |
98 class Mixer::Group { | 131 class Mixer::Group { |
99 public: | 132 public: |
100 Group(size_t max_results, double boost) | 133 Group(size_t max_results, double boost) |
101 : max_results_(max_results), | 134 : max_results_(max_results), |
102 boost_(boost) { | 135 boost_(boost) { |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
228 | 261 |
229 void Mixer::FetchResults(const KnownResults& known_results) { | 262 void Mixer::FetchResults(const KnownResults& known_results) { |
230 for (Groups::iterator group_it = groups_.begin(); | 263 for (Groups::iterator group_it = groups_.begin(); |
231 group_it != groups_.end(); | 264 group_it != groups_.end(); |
232 ++group_it) { | 265 ++group_it) { |
233 (*group_it)->FetchResults(known_results); | 266 (*group_it)->FetchResults(known_results); |
234 } | 267 } |
235 } | 268 } |
236 | 269 |
237 } // namespace app_list | 270 } // namespace app_list |
OLD | NEW |