OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef EXTENSIONS_COMMON_VALUE_COUNTER_H_ | 5 #ifndef EXTENSIONS_COMMON_VALUE_COUNTER_H_ |
6 #define EXTENSIONS_COMMON_VALUE_COUNTER_H_ | 6 #define EXTENSIONS_COMMON_VALUE_COUNTER_H_ |
7 | 7 |
8 #include <vector> | 8 #include "base/memory/scoped_vector.h" |
9 | |
10 #include "base/memory/linked_ptr.h" | |
11 | 9 |
12 namespace base { | 10 namespace base { |
13 class Value; | 11 class Value; |
14 } | 12 } |
15 | 13 |
16 namespace extensions { | 14 namespace extensions { |
17 | 15 |
18 // Keeps a running count of Values, like map<Value, int>. Adding / removing | 16 // Keeps a running count of Values, like map<Value, int>. Adding/removing |
19 // values increments / decrements the count associated with a given Value. | 17 // values increments/decrements the count associated with a given Value. |
20 // | 18 // |
21 // Add() and Remove() are linear in the number of Values in the ValueCounter, | 19 // Add() and Remove() are linear in the number of Values in the ValueCounter, |
22 // because there is no operator<() defined on Value, so we must iterate to find | 20 // because there is no operator<() defined on Value, so we must iterate to find |
23 // whether a Value is equal to an existing one. | 21 // whether a Value is equal to an existing one. |
24 class ValueCounter { | 22 class ValueCounter { |
25 public: | 23 public: |
26 ValueCounter(); | 24 ValueCounter(); |
27 ~ValueCounter(); | 25 ~ValueCounter(); |
28 | 26 |
29 // Adds |value| to the set and returns how many equal values are in the set | 27 // Adds |value| to the set. In the case where a Value equal to |value| |
30 // after. Does not take ownership of |value|. In the case where a Value equal | 28 // doesn't already exist in this map, this function makes a copy of |value| |
31 // to |value| doesn't already exist in this map, this function makes a | 29 // and returns true. Otherwise, it returns false. |
32 // DeepCopy() of |value|. | 30 bool Add(const base::Value& value); |
33 int Add(const base::Value& value); | |
34 | 31 |
35 // Removes |value| from the set and returns how many equal values are in | 32 // Removes |value| from the set, and returns true if it removed the last |
36 // the set after. | 33 // value equal to |value|. If there are more equal values, or if there |
37 int Remove(const base::Value& value); | 34 // weren't any in the first place, returns false. |
| 35 bool Remove(const base::Value& value); |
38 | 36 |
39 // Same as Add() but only performs the add if the value isn't present. | 37 // Returns true if there are no values of any type being counted. |
40 int AddIfMissing(const base::Value& value); | 38 bool is_empty() const { return entries_.empty(); } |
41 | 39 |
42 private: | 40 private: |
43 class Entry { | 41 struct Entry; |
44 public: | 42 ScopedVector<Entry> entries_; |
45 explicit Entry(const base::Value& value); | |
46 ~Entry(); | |
47 | |
48 int Increment(); | |
49 int Decrement(); | |
50 | |
51 const base::Value* value() const { return value_.get(); } | |
52 int count() const { return count_; } | |
53 | |
54 private: | |
55 linked_ptr<base::Value> value_; | |
56 int count_; | |
57 | |
58 DISALLOW_COPY_AND_ASSIGN(Entry); | |
59 }; | |
60 typedef std::vector<linked_ptr<Entry> > EntryList; | |
61 | |
62 int AddImpl(const base::Value& value, bool increment); | |
63 | |
64 EntryList entries_; | |
65 | 43 |
66 DISALLOW_COPY_AND_ASSIGN(ValueCounter); | 44 DISALLOW_COPY_AND_ASSIGN(ValueCounter); |
67 }; | 45 }; |
68 | 46 |
69 } // namespace extensions | 47 } // namespace extensions |
70 | 48 |
71 #endif // EXTENSIONS_COMMON_VALUE_COUNTER_H_ | 49 #endif // EXTENSIONS_COMMON_VALUE_COUNTER_H_ |
OLD | NEW |