OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "components/metrics/leak_detector/ranked_list.h" | |
6 | |
7 namespace leak_detector { | |
8 | |
9 void RankedList::Add(const ValueType& value, int count) { | |
10 // Determine where to insert the value. | |
11 int index = GetInsertionIndex(count); | |
12 if (index == -1) | |
13 return; | |
14 | |
15 entries_.emplace(entries_.begin() + index, Entry({count, value})); | |
16 | |
17 // Limit the list size if it exceeds the maximum allowed size. | |
18 if (entries_.size() > max_size_) | |
jar (doing other things)
2015/08/21 02:48:43
Wouldn't this test be better (with slight adjustme
Simon Que
2015/08/23 23:30:53
No longer an issue with list implementation.
| |
19 entries_.resize(max_size_); | |
20 } | |
21 | |
22 int RankedList::GetInsertionIndex(int count) const { | |
jar (doing other things)
2015/08/21 02:48:43
Have you considered using std::binary_search?
The
Simon Que
2015/08/21 05:44:05
Yes I could consider a linear search.
Simon Que
2015/08/23 23:30:53
Replaced with std::upper_bound.
| |
23 // If the list is full, do not add any entry with |count| if does not exceed | |
24 // the lowest count of the entries in the list. | |
25 if (size() == max_size_ && count <= min_count()) | |
26 return -1; | |
27 // If larger than the current largest item, insert it before. | |
28 if (count >= max_count()) | |
29 return 0; | |
30 // If smaller than the current smallest item, but the list is not full, | |
31 // insert it after. | |
32 if (count <= min_count()) | |
33 return size(); | |
34 | |
35 // Otherwise it's somewhere in the middle, so look for it there. | |
jar (doing other things)
2015/08/21 02:48:43
nit: undent
Simon Que
2015/08/21 21:59:20
Done.
| |
36 int min_pos = 0; | |
37 int max_pos = size() - 1; | |
38 while (max_pos >= min_pos) { | |
39 int midpoint = (max_pos + min_pos) / 2; | |
40 if (count >= entries_[midpoint].count && | |
41 count <= entries_[midpoint - 1].count) { | |
42 return midpoint; | |
43 } | |
44 // The list is reverse sorted. | |
45 if (entries_[midpoint].count >= count) | |
46 min_pos = midpoint + 1; | |
47 else | |
48 max_pos = midpoint - 1; | |
49 } | |
50 return max_pos; | |
51 } | |
52 | |
53 } // namespace leak_detector | |
OLD | NEW |