OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 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 _RANKED_LIST_H_ |
| 6 #define _RANKED_LIST_H_ |
| 7 |
| 8 #include <vector> |
| 9 |
| 10 #include "base/basictypes.h" |
| 11 #include "base/custom_allocator.h" |
| 12 #include "base/stl_allocator.h" |
| 13 #include "leak_detector_value_type.h" |
| 14 |
| 15 // RankedList lets you add entries and automatically sorts them internally, so |
| 16 // they can be accessed in sorted order. The entries are stored as a vector |
| 17 // array. |
| 18 |
| 19 namespace leak_detector { |
| 20 |
| 21 class RankedList { |
| 22 public: |
| 23 using ValueType = LeakDetectorValueType; |
| 24 |
| 25 // A single entry in the RankedList. The RankedList sorts entries by |count|. |
| 26 struct Entry { |
| 27 int count; |
| 28 ValueType value; |
| 29 }; |
| 30 |
| 31 RankedList(size_t max_size) : max_size_(max_size) { |
| 32 entries_.reserve(max_size + 1); |
| 33 } |
| 34 ~RankedList() {} |
| 35 |
| 36 // Access individual element. Does not do boundary checking. |
| 37 const Entry& entry(int index) const { |
| 38 return entries_[index]; |
| 39 } |
| 40 size_t size() const { |
| 41 return entries_.size(); |
| 42 } |
| 43 |
| 44 // Add a new value-count pair to the list. Does not check for existing entries |
| 45 // with the same value. |
| 46 void Add(const ValueType& value, int count); |
| 47 |
| 48 private: |
| 49 // Returns the position to insert a new value. Returns -1 if it cannot be |
| 50 // inserted. |
| 51 int GetInsertionIndex(int count) const; |
| 52 |
| 53 // Max and min counts. |
| 54 const int max_count() const { |
| 55 return entries_.empty() ? 0 : entries_[0].count; |
| 56 } |
| 57 const int min_count() const { |
| 58 return entries_.empty() ? 0 : entries_[size() - 1].count; |
| 59 } |
| 60 |
| 61 // Max number of items in the list. |
| 62 size_t max_size_; |
| 63 |
| 64 // Points to the array of entries. |
| 65 std::vector<Entry, STL_Allocator<Entry, CustomAllocator>> entries_; |
| 66 }; |
| 67 |
| 68 } // namespace leak_detector |
| 69 |
| 70 #endif // _RANKED_LIST_H_ |
OLD | NEW |