Chromium Code Reviews| Index: chrome/common/extensions/value_counter.cc |
| diff --git a/chrome/common/extensions/value_counter.cc b/chrome/common/extensions/value_counter.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..31a8f750e2cc32717558dc16d74291832ee3000c |
| --- /dev/null |
| +++ b/chrome/common/extensions/value_counter.cc |
| @@ -0,0 +1,64 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/common/extensions/value_counter.h" |
| + |
| +#include "base/values.h" |
| + |
| +namespace extensions { |
| + |
| +ValueCounter::ValueCounter() { |
| +} |
| + |
| +ValueCounter::~ValueCounter() { |
| +} |
| + |
| +ValueCounter::Entry::Entry(const base::Value& value) |
| + : value_(value.DeepCopy()), |
| + count_(1) { |
| +} |
| + |
| +ValueCounter::Entry::~Entry() { |
| +} |
| + |
| +int ValueCounter::Entry::Increment() { |
| + return ++count_; |
| +} |
| + |
| +int ValueCounter::Entry::Decrement() { |
| + return --count_; |
| +} |
| + |
| +int ValueCounter::Add(const base::Value& value) { |
| + return AddImpl(value, true); |
| +} |
| + |
| +int ValueCounter::Remove(const base::Value& value) { |
| + for (EntryList::iterator it = entries_.begin(); it != entries_.end(); it++) { |
| + (*it)->value()->GetType(); |
| + if ((*it)->value()->Equals(&value)) { |
| + int remaining = (*it)->Decrement(); |
| + if (remaining == 0) { |
| + entries_.erase(it); |
|
battre
2012/06/20 09:50:46
swap it with entries_.back() and pop_back()?
nit:
koz (OOO until 15th September)
2012/06/21 02:23:11
Done.
|
| + } |
| + return remaining; |
| + } |
| + } |
| + return 0; |
| +} |
| + |
| +int ValueCounter::AddIfMissing(const base::Value& value) { |
| + return AddImpl(value, false); |
| +} |
| + |
| +int ValueCounter::AddImpl(const base::Value& value, bool increment) { |
| + for (EntryList::iterator it = entries_.begin(); it != entries_.end(); it++) { |
| + if ((*it)->value()->Equals(&value)) |
| + return increment ? (*it)->Increment() : (*it)->count(); |
| + } |
| + entries_.push_back(linked_ptr<Entry>(new Entry(value))); |
| + return 1; |
| +} |
| + |
| +} // namespace extensions |