OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 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 CHROME_COMMON_EXTENSIONS_VALUE_COUNTER_H_ |
| 6 #define CHROME_COMMON_EXTENSIONS_VALUE_COUNTER_H_ |
| 7 #pragma once |
| 8 |
| 9 #include "base/memory/linked_ptr.h" |
| 10 |
| 11 #include <vector> |
| 12 |
| 13 namespace base { |
| 14 class Value; |
| 15 } |
| 16 |
| 17 namespace extensions { |
| 18 |
| 19 // Keeps a running count of Values, like map<Value, int>. Adding / removing |
| 20 // values increments / decrements the count associated with a given Value. |
| 21 // |
| 22 // Add() and Remove() are linear in the number of Values in the ValueCounter, |
| 23 // because there is no operator<() defined on Value, so we must iterate to find |
| 24 // whether a Value is equal to an existing one. |
| 25 class ValueCounter { |
| 26 public: |
| 27 ValueCounter(); |
| 28 ~ValueCounter(); |
| 29 |
| 30 // Adds |value| to the set and returns how many equal values are in the set |
| 31 // after. Does not take ownership of |value|. In the case where a Value equal |
| 32 // to |value| doesn't already exist in this map, this function makes a |
| 33 // DeepCopy() of |value|. |
| 34 int Add(const base::Value& value); |
| 35 |
| 36 // Removes |value| from the set and returns how many equal values are in |
| 37 // the set after. |
| 38 int Remove(const base::Value& value); |
| 39 |
| 40 // Same as Add() but only performs the add if the value isn't present. |
| 41 int AddIfMissing(const base::Value& value); |
| 42 |
| 43 private: |
| 44 class Entry { |
| 45 public: |
| 46 explicit Entry(const base::Value& value); |
| 47 ~Entry(); |
| 48 |
| 49 int Increment(); |
| 50 int Decrement(); |
| 51 |
| 52 const base::Value* value() const { return value_.get(); } |
| 53 int count() const { return count_; } |
| 54 |
| 55 private: |
| 56 linked_ptr<base::Value> value_; |
| 57 int count_; |
| 58 |
| 59 DISALLOW_COPY_AND_ASSIGN(Entry); |
| 60 }; |
| 61 typedef std::vector<linked_ptr<Entry> > EntryList; |
| 62 |
| 63 int AddImpl(const base::Value& value, bool increment); |
| 64 |
| 65 EntryList entries_; |
| 66 |
| 67 DISALLOW_COPY_AND_ASSIGN(ValueCounter); |
| 68 }; |
| 69 |
| 70 } // namespace extensions |
| 71 |
| 72 #endif // CHROME_COMMON_EXTENSIONS_VALUE_COUNTER_H_ |
OLD | NEW |