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 { |
| 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. |
| 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; |
| 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 |