Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / extensions / common / value_counter.h
blob03f7907f8353caf964f70694c1bfb1c8e7e35ef6
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
3 // found in the LICENSE file.
5 #ifndef EXTENSIONS_COMMON_VALUE_COUNTER_H_
6 #define EXTENSIONS_COMMON_VALUE_COUNTER_H_
8 #include "base/memory/scoped_vector.h"
10 namespace base {
11 class Value;
14 namespace extensions {
16 // Keeps a running count of Values, like map<Value, int>. Adding/removing
17 // values increments/decrements the count associated with a given Value.
19 // Add() and Remove() are linear in the number of Values in the ValueCounter,
20 // because there is no operator<() defined on Value, so we must iterate to find
21 // whether a Value is equal to an existing one.
22 class ValueCounter {
23 public:
24 ValueCounter();
25 ~ValueCounter();
27 // Adds |value| to the set. In the case where a Value equal to |value|
28 // doesn't already exist in this map, this function makes a copy of |value|
29 // and returns true. Otherwise, it returns false.
30 bool Add(const base::Value& value);
32 // Removes |value| from the set, and returns true if it removed the last
33 // value equal to |value|. If there are more equal values, or if there
34 // weren't any in the first place, returns false.
35 bool Remove(const base::Value& value);
37 // Returns true if there are no values of any type being counted.
38 bool is_empty() const { return entries_.empty(); }
40 private:
41 struct Entry;
42 ScopedVector<Entry> entries_;
44 DISALLOW_COPY_AND_ASSIGN(ValueCounter);
47 } // namespace extensions
49 #endif // EXTENSIONS_COMMON_VALUE_COUNTER_H_