Chromium Code Reviews| Index: third_party/tcmalloc/chromium/src/ranked_list.h |
| diff --git a/third_party/tcmalloc/chromium/src/ranked_list.h b/third_party/tcmalloc/chromium/src/ranked_list.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..87a0a59139e0cf88a3f039b85412bd74499b45fa |
| --- /dev/null |
| +++ b/third_party/tcmalloc/chromium/src/ranked_list.h |
| @@ -0,0 +1,117 @@ |
| +// Copyright (c) 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +// --- |
| +// Author: Simon Que |
| + |
| +#ifndef _RANKED_LIST_H_ |
| +#define _RANKED_LIST_H_ |
| + |
| +#include <vector> |
| + |
| +#include "base/basictypes.h" |
| +#include "base/custom_allocator.h" |
| +#include "base/stl_allocator.h" |
| + |
| +// RankedList lets you add entries and automatically sorts them internally, so |
| +// they can be accessed in sorted order. The entries are stored as a vector |
| +// array. |
| + |
| +namespace leak_detector { |
| + |
| +template <typename T> |
| +class RankedList { |
| + public: |
| + // A single entry in the RankedList. The RankedList sorts entries by |count|. |
| + struct Entry { |
| + int count; |
| + T value; |
| + }; |
| + |
| + RankedList(size_t max_size) : max_size_(max_size) { |
| + entries_.reserve(max_size + 1); |
| + } |
| + ~RankedList() {} |
| + |
| + // Access individual element. Does not do boundary checking. |
| + const Entry& entry(int index) const { |
| + return entries_[index]; |
| + } |
| + size_t size() const { |
| + return entries_.size(); |
| + } |
| + |
| + // Add a new value-count pair to the list. Does not check for existing entries |
| + // with the same value. |
| + void Add(const T& value, int count); |
| + |
| + private: |
| + // Returns the position to insert a new value. Returns -1 if it cannot be |
| + // inserted. |
| + int GetInsertionIndex(int count) const; |
| + |
| + // Max and min counts. |
| + const int max_count() const { |
| + return entries_.empty() ? 0 : entries_[0].count; |
| + } |
| + const int min_count() const { |
| + return entries_.empty() ? 0 : entries_[size() - 1].count; |
| + } |
| + |
| + // Max number of items in the list. |
| + size_t max_size_; |
| + |
| + // Points to the array of entries. |
| + std::vector<Entry, STL_Allocator<Entry, CustomAllocator>> entries_; |
| +}; |
| + |
| +template <typename T> |
| +void RankedList<T>::Add(const T& value, int count) { |
| + // Determine where to insert the value. |
| + int index = GetInsertionIndex(count); |
| + if (index == -1) |
| + return; |
| + |
| + entries_.emplace(entries_.begin() + index, Entry({count, value})); |
| + |
| + // Limit the list size if it exceeds the maximum allowed size. |
| + if (entries_.size() > max_size_) |
| + entries_.resize(max_size_); |
| +} |
| + |
| +template <typename T> |
| +int RankedList<T>::GetInsertionIndex(int count) const { |
|
jar (doing other things)
2015/07/20 22:19:03
I'm again a bit surprised to see larger code block
|
| + // If the list is full, do not add any entry with |count| if does not exceed |
| + // the lowest count of the entries in the list. |
| + if (size() == max_size_ && count <= min_count()) |
| + return -1; |
| + // If larger than the current largest item, insert it before. |
| + if (count >= max_count()) |
| + return 0; |
| + // If smaller than the current smallest item, but the list is not full, |
| + // insert it after. |
| + if (count <= min_count()) |
| + return size(); |
| + |
| + // Otherwise it's somewhere in the middle, so look for it there. |
| + int min_pos = 0; |
| + int max_pos = size() - 1; |
| + while (max_pos >= min_pos) { |
| + int midpoint = (max_pos + min_pos) / 2; |
| + if (count >= entries_[midpoint].count && |
| + count <= entries_[midpoint - 1].count) { |
| + return midpoint; |
| + } |
| + // The list is reverse sorted. |
| + if (entries_[midpoint].count >= count) |
| + min_pos = midpoint + 1; |
| + else |
| + max_pos = midpoint - 1; |
| + } |
| + return max_pos; |
| +} |
| + |
| +} // namespace leak_detector |
| + |
| +#endif // _RANKED_LIST_H_ |