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 <stddef.h> |
| 9 |
| 10 #include <list> |
| 11 |
| 12 #include "base/macros.h" |
| 13 #include "base/move.h" |
| 14 #include "components/metrics/leak_detector/custom_allocator.h" |
| 15 #include "components/metrics/leak_detector/leak_detector_value_type.h" |
| 16 #include "components/metrics/leak_detector/stl_allocator.h" |
| 17 |
| 18 namespace metrics { |
| 19 namespace leak_detector { |
| 20 |
| 21 // RankedList lets you add entries consisting of a value-count pair, and |
| 22 // automatically sorts them internally by count in descending order. This allows |
| 23 // for the user of this list to put value-count pairs into this list without |
| 24 // having to explicitly sort them by count. |
| 25 class RankedList { |
| 26 MOVE_ONLY_TYPE_FOR_CPP_03(RankedList, RValue); |
| 27 |
| 28 public: |
| 29 using ValueType = LeakDetectorValueType; |
| 30 |
| 31 // A single entry in the RankedList. The RankedList sorts entries by |count| |
| 32 // in descending order. |
| 33 struct Entry { |
| 34 ValueType value; |
| 35 int count; |
| 36 |
| 37 // Create a < comparator for reverse sorting. |
| 38 bool operator<(const Entry& entry) const { return count > entry.count; } |
| 39 }; |
| 40 |
| 41 using EntryList = std::list<Entry, STLAllocator<Entry, CustomAllocator>>; |
| 42 using const_iterator = EntryList::const_iterator; |
| 43 |
| 44 explicit RankedList(size_t max_size); |
| 45 ~RankedList(); |
| 46 |
| 47 // For move semantics. |
| 48 RankedList(RValue other); |
| 49 RankedList& operator=(RValue other); |
| 50 |
| 51 // Accessors for begin() and end() const iterators. |
| 52 const_iterator begin() const { return entries_.begin(); } |
| 53 const_iterator end() const { return entries_.end(); } |
| 54 |
| 55 size_t size() const { return entries_.size(); } |
| 56 size_t max_size() const { return max_size_; } |
| 57 |
| 58 // Add a new value-count pair to the list. Does not check for existing entries |
| 59 // with the same value. Is an O(n) operation due to ordering. |
| 60 void Add(const ValueType& value, int count); |
| 61 |
| 62 private: |
| 63 // Max and min counts. Returns 0 if the list is empty. |
| 64 int max_count() const { |
| 65 return entries_.empty() ? 0 : entries_.begin()->count; |
| 66 } |
| 67 int min_count() const { |
| 68 return entries_.empty() ? 0 : entries_.rbegin()->count; |
| 69 } |
| 70 |
| 71 // Max number of items that can be stored in the list. |
| 72 size_t max_size_; |
| 73 |
| 74 // Points to the array of entries. |
| 75 std::list<Entry, STLAllocator<Entry, CustomAllocator>> entries_; |
| 76 }; |
| 77 |
| 78 } // namespace leak_detector |
| 79 } // namespace metrics |
| 80 |
| 81 #endif // COMPONENTS_METRICS_LEAK_DETECTOR_RANKED_LIST_H_ |
OLD | NEW |