| Index: extensions/common/value_counter.cc
|
| diff --git a/extensions/common/value_counter.cc b/extensions/common/value_counter.cc
|
| index 4138f251895981b39568d5eed7cb3fc90b27491f..0266b9ac8f95c93ee2095216849d1d73b1320a8b 100644
|
| --- a/extensions/common/value_counter.cc
|
| +++ b/extensions/common/value_counter.cc
|
| @@ -10,47 +10,44 @@
|
|
|
| namespace extensions {
|
|
|
| -ValueCounter::ValueCounter() {}
|
| +struct ValueCounter::Entry {
|
| + explicit Entry(scoped_ptr<base::Value> value)
|
| + : value(value.Pass()), count(1) {}
|
|
|
| -ValueCounter::~ValueCounter() {}
|
| + scoped_ptr<base::Value> value;
|
| + int count;
|
| +};
|
|
|
| -ValueCounter::Entry::Entry(const base::Value& value)
|
| - : value_(value.DeepCopy()), count_(1) {}
|
| -
|
| -ValueCounter::Entry::~Entry() {}
|
| -
|
| -int ValueCounter::Entry::Increment() { return ++count_; }
|
| +ValueCounter::ValueCounter() {
|
| +}
|
|
|
| -int ValueCounter::Entry::Decrement() { return --count_; }
|
| +ValueCounter::~ValueCounter() {
|
| +}
|
|
|
| -int ValueCounter::Add(const base::Value& value) { return AddImpl(value, true); }
|
| +bool ValueCounter::Add(const base::Value& value) {
|
| + for (Entry* entry : entries_) {
|
| + if (entry->value->Equals(&value)) {
|
| + ++entry->count;
|
| + return false;
|
| + }
|
| + }
|
| + entries_.push_back(new Entry(value.CreateDeepCopy()));
|
| + return 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) {
|
| +bool ValueCounter::Remove(const base::Value& value) {
|
| + for (ScopedVector<Entry>::iterator it = entries_.begin();
|
| + it != entries_.end(); ++it) {
|
| + if ((*it)->value->Equals(&value)) {
|
| + if (--(*it)->count == 0) {
|
| std::swap(*it, entries_.back());
|
| entries_.pop_back();
|
| + return true; // Removed the last entry.
|
| }
|
| - return remaining;
|
| + return false; // Removed, but no the last entry.
|
| }
|
| }
|
| - 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;
|
| + return false; // Nothing to remove.
|
| }
|
|
|
| } // namespace extensions
|
|
|