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