Chromium Code Reviews| Index: extensions/common/value_counter.cc |
| diff --git a/extensions/common/value_counter.cc b/extensions/common/value_counter.cc |
| index 4138f251895981b39568d5eed7cb3fc90b27491f..0bb9cec75b435f6277b06be35b42504d8f974a65 100644 |
| --- a/extensions/common/value_counter.cc |
| +++ b/extensions/common/value_counter.cc |
| @@ -10,47 +10,46 @@ |
| namespace extensions { |
| +struct ValueCounter::Entry { |
|
Devlin
2015/07/10 22:07:42
Can we make a constructor (passing in a value, ini
not at google - send to devlin
2015/07/10 22:42:43
Done.
|
| + scoped_ptr<base::Value> value; |
| + int count; |
| +}; |
| + |
| 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); } |
| +bool ValueCounter::Add(const base::Value& value) { |
| + for (Entry* entry : entries_) { |
| + if (entry->value->Equals(&value)) { |
| + ++entry->count; |
| + return false; |
| + } |
| + } |
| + scoped_ptr<Entry> entry(new Entry()); |
| + entry->value = value.CreateDeepCopy(); |
| + entry->count = 1; |
| + entries_.push_back(entry.Pass()); |
| + 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; |
| } |
| - return remaining; |
| + return false; |
|
Devlin
2015/07/10 22:07:42
These are right, but I feel like adding
return
not at google - send to devlin
2015/07/10 22:42:43
Done.
|
| } |
| } |
| - return 0; |
| + return false; |
| } |
| -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; |
| +bool ValueCounter::IsEmpty() const { |
|
Devlin
2015/07/10 22:07:42
Could probably move this to be is_empty() in the h
not at google - send to devlin
2015/07/10 22:42:43
Alright. I don't mind it being IsEmpty, it's consi
Devlin
2015/07/10 22:45:22
But those do Work, and this doesn't. :)
|
| + return entries_.empty(); |
| } |
| } // namespace extensions |