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< (Entry& entry) const { |
| 39 return count > entry.count; |
| 40 } |
| 41 }; |
| 42 |
| 43 using EntryList = std::list<Entry, STLAllocator<Entry, CustomAllocator>>; |
| 44 using const_iterator = EntryList::const_iterator; |
| 45 |
| 46 explicit RankedList(size_t max_size) : max_size_(max_size) {} |
| 47 |
| 48 // For move semantics. |
| 49 RankedList(RValue other); |
| 50 RankedList& operator= (RValue other); |
| 51 |
| 52 // Accessors for begin() and end() const iterators. |
| 53 const_iterator begin() const { |
| 54 return entries_.begin(); |
| 55 } |
| 56 const_iterator end() const { |
| 57 return entries_.end(); |
| 58 } |
| 59 |
| 60 size_t size() const { |
| 61 return entries_.size(); |
| 62 } |
| 63 size_t max_size() const { |
| 64 return max_size_; |
| 65 } |
| 66 |
| 67 // Add a new value-count pair to the list. Does not check for existing entries |
| 68 // with the same value. Is an O(n) operation due to ordering. |
| 69 void Add(const ValueType& value, int count); |
| 70 |
| 71 private: |
| 72 // Max and min counts. Returns 0 if the list is empty. |
| 73 const int max_count() const { |
| 74 return entries_.empty() ? 0 : entries_.begin()->count; |
| 75 } |
| 76 const int min_count() const { |
| 77 return entries_.empty() ? 0 : entries_.rbegin()->count; |
| 78 } |
| 79 |
| 80 // Max number of items that can be stored in the list. |
| 81 size_t max_size_; |
| 82 |
| 83 // Points to the array of entries. |
| 84 std::list<Entry, STLAllocator<Entry, CustomAllocator>> entries_; |
| 85 }; |
| 86 |
| 87 } // namespace leak_detector |
| 88 } // namespace metrics |
| 89 |
| 90 #endif // COMPONENTS_METRICS_LEAK_DETECTOR_RANKED_LIST_H_ |
OLD | NEW |