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 #ifndef COMPONENTS_METRICS_LEAK_DETECTOR_RANKED_LIST_H_ | |
6 #define COMPONENTS_METRICS_LEAK_DETECTOR_RANKED_LIST_H_ | |
7 | |
8 #include <stdint.h> | |
9 | |
10 #include <vector> | |
11 | |
12 #include "components/metrics/leak_detector/leak_detector_value_type.h" | |
13 #include "components/metrics/leak_detector/stl_allocator.h" | |
14 #include <gperftools/custom_allocator.h> | |
15 | |
16 // RankedList lets you add entries and automatically sorts them internally, so | |
17 // they can be accessed in sorted order. The entries are stored as a vector | |
18 // array. | |
19 | |
20 namespace leak_detector { | |
21 | |
22 class RankedList { | |
jar (doing other things)
2015/08/21 02:48:43
This name is IMO slightly deceptive. A "list" usua
Simon Que
2015/08/21 21:59:20
I'm going to rethink this entire implementation an
Simon Que
2015/08/23 23:30:53
Done.
| |
23 public: | |
24 using ValueType = LeakDetectorValueType; | |
25 | |
26 // A single entry in the RankedList. The RankedList sorts entries by |count|. | |
27 struct Entry { | |
28 int count; | |
29 ValueType value; | |
30 }; | |
31 | |
32 explicit RankedList(size_t max_size) : max_size_(max_size) { | |
33 entries_.reserve(max_size + 1); | |
34 } | |
35 ~RankedList() {} | |
36 | |
37 // Access individual element. Does not do boundary checking. | |
38 const Entry& entry(int index) const { | |
39 return entries_[index]; | |
40 } | |
41 | |
42 size_t size() const { | |
43 return entries_.size(); | |
44 } | |
45 | |
46 // Add a new value-count pair to the list. Does not check for existing entries | |
47 // with the same value. | |
jar (doing other things)
2015/08/21 02:48:43
nit: Add comment that this is an O(N) insertion in
Simon Que
2015/08/23 23:30:53
Now a O(1) insertion into a linked list. :)
Simon Que
2015/08/31 05:47:03
Actually that's wrong... it's O(N) due to ordering
| |
48 void Add(const ValueType& value, int count); | |
49 | |
50 private: | |
51 // Returns the position to insert a new value. Returns -1 if it cannot be | |
52 // inserted. | |
53 int GetInsertionIndex(int count) const; | |
jar (doing other things)
2015/08/21 02:48:43
nit: Add comment that this is a binary search, of
Simon Que
2015/08/23 23:30:53
Removed this function, replaced with upper_bound (
| |
54 | |
55 // Max and min counts. Returns 0 if the list is empty. | |
56 const int max_count() const { | |
57 return entries_.empty() ? 0 : entries_[0].count; | |
58 } | |
59 const int min_count() const { | |
60 return entries_.empty() ? 0 : entries_[size() - 1].count; | |
61 } | |
62 | |
63 // Max number of items in the list. | |
64 size_t max_size_; | |
65 | |
66 // Points to the array of entries. | |
67 std::vector<Entry, STL_Allocator<Entry, CustomAllocator>> entries_; | |
68 }; | |
69 | |
70 } // namespace leak_detector | |
71 | |
72 #endif // COMPONENTS_METRICS_LEAK_DETECTOR_RANKED_LIST_H_ | |
OLD | NEW |