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