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 #include "components/metrics/leak_detector/ranked_list.h" | |
6 | |
7 #include <algorithm> | |
8 | |
9 namespace metrics { | |
10 namespace leak_detector { | |
11 | |
12 RankedList::RankedList(size_t max_size) : max_size_(max_size) {} | |
13 | |
14 RankedList::~RankedList() {} | |
15 | |
16 RankedList::RankedList(RankedList::RValue other) | |
17 : max_size_(other.object->max_size_) { | |
18 entries_.swap(other.object->entries_); | |
19 } | |
20 | |
21 RankedList& RankedList::operator=(RankedList::RValue other) { | |
22 max_size_ = other.object->max_size_; | |
23 entries_.swap(other.object->entries_); | |
24 return *this; | |
25 } | |
26 | |
27 void RankedList::Add(const ValueType& value, int count) { | |
28 // Determine where to insert the value given its count. | |
29 EntryList::iterator iter = std::upper_bound(entries_.begin(), entries_.end(), | |
30 Entry{ValueType(), count}); | |
31 | |
32 // If the list is full, do not add any entry with |count| if does not exceed | |
33 // the lowest count of the entries in the list. | |
34 if (size() == max_size_ && iter == end()) | |
35 return; | |
36 | |
37 entries_.insert(iter, Entry({value, count})); | |
38 | |
39 // Limit the list size if it exceeds the maximum allowed size. | |
40 if (entries_.size() > max_size_) | |
41 entries_.resize(max_size_); | |
42 } | |
43 | |
44 } // namespace leak_detector | |
45 } // namespace metrics | |
OLD | NEW |