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 "ui/app_list/search/mixer.h" | 5 #include "ui/app_list/search/mixer.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <map> | 8 #include <map> |
9 #include <set> | 9 #include <set> |
10 #include <string> | 10 #include <string> |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
53 public: | 53 public: |
54 Group(size_t max_results, double boost) | 54 Group(size_t max_results, double boost) |
55 : max_results_(max_results), boost_(boost) {} | 55 : max_results_(max_results), boost_(boost) {} |
56 ~Group() {} | 56 ~Group() {} |
57 | 57 |
58 void AddProvider(SearchProvider* provider) { providers_.push_back(provider); } | 58 void AddProvider(SearchProvider* provider) { providers_.push_back(provider); } |
59 | 59 |
60 void FetchResults(bool is_voice_query, const KnownResults& known_results) { | 60 void FetchResults(bool is_voice_query, const KnownResults& known_results) { |
61 results_.clear(); | 61 results_.clear(); |
62 | 62 |
63 for (Providers::const_iterator provider_it = providers_.begin(); | 63 for (const SearchProvider* provider : providers_) { |
64 provider_it != providers_.end(); | 64 for (SearchResult* result : provider->results()) { |
65 ++provider_it) { | 65 DCHECK_GE(result->relevance(), 0.0); |
66 for (SearchProvider::Results::const_iterator result_it = | 66 DCHECK_LE(result->relevance(), 1.0); |
67 (*provider_it)->results().begin(); | 67 DCHECK(!result->id().empty()); |
68 result_it != (*provider_it)->results().end(); | |
69 ++result_it) { | |
70 DCHECK_GE((*result_it)->relevance(), 0.0); | |
71 DCHECK_LE((*result_it)->relevance(), 1.0); | |
72 DCHECK(!(*result_it)->id().empty()); | |
73 | 68 |
74 double boost = boost_; | 69 double boost = boost_; |
75 KnownResults::const_iterator known_it = | 70 KnownResults::const_iterator known_it = |
76 known_results.find((*result_it)->id()); | 71 known_results.find(result->id()); |
77 if (known_it != known_results.end()) { | 72 if (known_it != known_results.end()) { |
78 switch (known_it->second) { | 73 switch (known_it->second) { |
79 case PERFECT_PRIMARY: | 74 case PERFECT_PRIMARY: |
80 boost = 4.0; | 75 boost = 4.0; |
81 break; | 76 break; |
82 case PREFIX_PRIMARY: | 77 case PREFIX_PRIMARY: |
83 boost = 3.75; | 78 boost = 3.75; |
84 break; | 79 break; |
85 case PERFECT_SECONDARY: | 80 case PERFECT_SECONDARY: |
86 boost = 3.25; | 81 boost = 3.25; |
87 break; | 82 break; |
88 case PREFIX_SECONDARY: | 83 case PREFIX_SECONDARY: |
89 boost = 3.0; | 84 boost = 3.0; |
90 break; | 85 break; |
91 case UNKNOWN_RESULT: | 86 case UNKNOWN_RESULT: |
92 NOTREACHED() << "Unknown result in KnownResults?"; | 87 NOTREACHED() << "Unknown result in KnownResults?"; |
93 break; | 88 break; |
94 } | 89 } |
95 } | 90 } |
96 | 91 |
97 // If this is a voice query, voice results receive a massive boost. | 92 // If this is a voice query, voice results receive a massive boost. |
98 if (is_voice_query && (*result_it)->voice_result()) | 93 if (is_voice_query && result->voice_result()) |
99 boost += 4.0; | 94 boost += 4.0; |
100 | 95 |
101 results_.push_back( | 96 results_.push_back(SortData(result, result->relevance() + boost)); |
102 SortData(*result_it, (*result_it)->relevance() + boost)); | |
103 } | 97 } |
104 } | 98 } |
105 | 99 |
106 std::sort(results_.begin(), results_.end()); | 100 std::sort(results_.begin(), results_.end()); |
107 if (max_results_ != kNoMaxResultsLimit && results_.size() > max_results_) | 101 if (max_results_ != kNoMaxResultsLimit && results_.size() > max_results_) |
108 results_.resize(max_results_); | 102 results_.resize(max_results_); |
109 } | 103 } |
110 | 104 |
111 const SortedResults& results() const { return results_; } | 105 const SortedResults& results() const { return results_; } |
112 | 106 |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
198 IdToResultMap ui_results_map; | 192 IdToResultMap ui_results_map; |
199 for (size_t i = 0; i < ui_results->item_count(); ++i) { | 193 for (size_t i = 0; i < ui_results->item_count(); ++i) { |
200 SearchResult* ui_result = ui_results->GetItemAt(i); | 194 SearchResult* ui_result = ui_results->GetItemAt(i); |
201 ui_results_map[ui_result->id()] = ui_result; | 195 ui_results_map[ui_result->id()] = ui_result; |
202 } | 196 } |
203 // We have to erase all results at once so that observers are notified with | 197 // We have to erase all results at once so that observers are notified with |
204 // meaningful indexes. | 198 // meaningful indexes. |
205 ui_results->RemoveAll(); | 199 ui_results->RemoveAll(); |
206 | 200 |
207 // Add items back to |ui_results| in the order of |new_results|. | 201 // Add items back to |ui_results| in the order of |new_results|. |
208 for (size_t i = 0; i < new_results.size(); ++i) { | 202 for (const SortData& sort_data : new_results) { |
209 const SearchResult& new_result = *new_results[i].result; | 203 const SearchResult& new_result = *sort_data.result; |
210 IdToResultMap::const_iterator ui_result_it = | 204 IdToResultMap::const_iterator ui_result_it = |
211 ui_results_map.find(new_result.id()); | 205 ui_results_map.find(new_result.id()); |
212 if (ui_result_it != ui_results_map.end()) { | 206 if (ui_result_it != ui_results_map.end()) { |
213 // Update and use the old result if it exists. | 207 // Update and use the old result if it exists. |
214 SearchResult* ui_result = ui_result_it->second; | 208 SearchResult* ui_result = ui_result_it->second; |
215 UpdateResult(new_result, ui_result); | 209 UpdateResult(new_result, ui_result); |
216 | 210 |
217 // |ui_results| takes back ownership from |ui_results_map| here. | 211 // |ui_results| takes back ownership from |ui_results_map| here. |
218 ui_results->Add(ui_result); | 212 ui_results->Add(ui_result); |
219 | 213 |
220 // Remove the item from the map so that it ends up only with unused | 214 // Remove the item from the map so that it ends up only with unused |
221 // results. | 215 // results. |
222 ui_results_map.erase(ui_result->id()); | 216 ui_results_map.erase(ui_result->id()); |
223 } else { | 217 } else { |
224 // Copy the result from |new_results| otherwise. | 218 // Copy the result from |new_results| otherwise. |
225 ui_results->Add(new_result.Duplicate().release()); | 219 ui_results->Add(new_result.Duplicate().release()); |
226 } | 220 } |
227 } | 221 } |
228 | 222 |
229 // Delete the results remaining in the map as they are not in the new results. | 223 // Delete the results remaining in the map as they are not in the new results. |
230 for (IdToResultMap::const_iterator ui_result_it = ui_results_map.begin(); | 224 for (const auto& ui_result : ui_results_map) { |
231 ui_result_it != ui_results_map.end(); | 225 delete ui_result.second; |
232 ++ui_result_it) { | |
233 delete ui_result_it->second; | |
234 } | 226 } |
235 } | 227 } |
236 | 228 |
237 void Mixer::RemoveDuplicates(SortedResults* results) { | 229 void Mixer::RemoveDuplicates(SortedResults* results) { |
238 SortedResults final; | 230 SortedResults final; |
239 final.reserve(results->size()); | 231 final.reserve(results->size()); |
240 | 232 |
241 std::set<std::string> id_set; | 233 std::set<std::string> id_set; |
242 for (SortedResults::iterator it = results->begin(); it != results->end(); | 234 for (const SortData& sort_data : *results) { |
243 ++it) { | 235 const std::string& id = sort_data.result->id(); |
244 const std::string& id = it->result->id(); | |
245 if (id_set.find(id) != id_set.end()) | 236 if (id_set.find(id) != id_set.end()) |
246 continue; | 237 continue; |
247 | 238 |
248 id_set.insert(id); | 239 id_set.insert(id); |
249 final.push_back(*it); | 240 final.push_back(sort_data); |
250 } | 241 } |
251 | 242 |
252 results->swap(final); | 243 results->swap(final); |
253 } | 244 } |
254 | 245 |
255 void Mixer::FetchResults(bool is_voice_query, | 246 void Mixer::FetchResults(bool is_voice_query, |
256 const KnownResults& known_results) { | 247 const KnownResults& known_results) { |
257 for (const auto& item : groups_) | 248 for (const auto& item : groups_) |
258 item.second->FetchResults(is_voice_query, known_results); | 249 item.second->FetchResults(is_voice_query, known_results); |
259 } | 250 } |
260 | 251 |
261 } // namespace app_list | 252 } // namespace app_list |
OLD | NEW |