Upstreaming browser/ui/uikit_ui_util from iOS.
[chromium-blink-merge.git] / extensions / common / value_counter.cc
blob0266b9ac8f95c93ee2095216849d1d73b1320a8b
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 #include "extensions/common/value_counter.h"
7 #include <algorithm>
9 #include "base/values.h"
11 namespace extensions {
13 struct ValueCounter::Entry {
14 explicit Entry(scoped_ptr<base::Value> value)
15 : value(value.Pass()), count(1) {}
17 scoped_ptr<base::Value> value;
18 int count;
21 ValueCounter::ValueCounter() {
24 ValueCounter::~ValueCounter() {
27 bool ValueCounter::Add(const base::Value& value) {
28 for (Entry* entry : entries_) {
29 if (entry->value->Equals(&value)) {
30 ++entry->count;
31 return false;
34 entries_.push_back(new Entry(value.CreateDeepCopy()));
35 return true;
38 bool ValueCounter::Remove(const base::Value& value) {
39 for (ScopedVector<Entry>::iterator it = entries_.begin();
40 it != entries_.end(); ++it) {
41 if ((*it)->value->Equals(&value)) {
42 if (--(*it)->count == 0) {
43 std::swap(*it, entries_.back());
44 entries_.pop_back();
45 return true; // Removed the last entry.
47 return false; // Removed, but no the last entry.
50 return false; // Nothing to remove.
53 } // namespace extensions