Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "extensions/common/value_counter.h" | 5 #include "extensions/common/value_counter.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/values.h" | 9 #include "base/values.h" |
| 10 | 10 |
| 11 namespace extensions { | 11 namespace extensions { |
| 12 | 12 |
| 13 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.
| |
| 14 scoped_ptr<base::Value> value; | |
| 15 int count; | |
| 16 }; | |
| 17 | |
| 13 ValueCounter::ValueCounter() {} | 18 ValueCounter::ValueCounter() {} |
| 14 | 19 |
| 15 ValueCounter::~ValueCounter() {} | 20 ValueCounter::~ValueCounter() {} |
| 16 | 21 |
| 17 ValueCounter::Entry::Entry(const base::Value& value) | 22 bool ValueCounter::Add(const base::Value& value) { |
| 18 : value_(value.DeepCopy()), count_(1) {} | 23 for (Entry* entry : entries_) { |
| 24 if (entry->value->Equals(&value)) { | |
| 25 ++entry->count; | |
| 26 return false; | |
| 27 } | |
| 28 } | |
| 29 scoped_ptr<Entry> entry(new Entry()); | |
| 30 entry->value = value.CreateDeepCopy(); | |
| 31 entry->count = 1; | |
| 32 entries_.push_back(entry.Pass()); | |
| 33 return true; | |
| 34 } | |
| 19 | 35 |
| 20 ValueCounter::Entry::~Entry() {} | 36 bool ValueCounter::Remove(const base::Value& value) { |
| 21 | 37 for (ScopedVector<Entry>::iterator it = entries_.begin(); |
| 22 int ValueCounter::Entry::Increment() { return ++count_; } | 38 it != entries_.end(); ++it) { |
| 23 | 39 if ((*it)->value->Equals(&value)) { |
| 24 int ValueCounter::Entry::Decrement() { return --count_; } | 40 if (--(*it)->count == 0) { |
| 25 | |
| 26 int ValueCounter::Add(const base::Value& value) { return AddImpl(value, true); } | |
| 27 | |
| 28 int ValueCounter::Remove(const base::Value& value) { | |
| 29 for (EntryList::iterator it = entries_.begin(); it != entries_.end(); it++) { | |
| 30 (*it)->value()->GetType(); | |
| 31 if ((*it)->value()->Equals(&value)) { | |
| 32 int remaining = (*it)->Decrement(); | |
| 33 if (remaining == 0) { | |
| 34 std::swap(*it, entries_.back()); | 41 std::swap(*it, entries_.back()); |
| 35 entries_.pop_back(); | 42 entries_.pop_back(); |
| 43 return true; | |
| 36 } | 44 } |
| 37 return remaining; | 45 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.
| |
| 38 } | 46 } |
| 39 } | 47 } |
| 40 return 0; | 48 return false; |
| 41 } | 49 } |
| 42 | 50 |
| 43 int ValueCounter::AddIfMissing(const base::Value& value) { | 51 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. :)
| |
| 44 return AddImpl(value, false); | 52 return entries_.empty(); |
| 45 } | |
| 46 | |
| 47 int ValueCounter::AddImpl(const base::Value& value, bool increment) { | |
| 48 for (EntryList::iterator it = entries_.begin(); it != entries_.end(); it++) { | |
| 49 if ((*it)->value()->Equals(&value)) | |
| 50 return increment ? (*it)->Increment() : (*it)->count(); | |
| 51 } | |
| 52 entries_.push_back(linked_ptr<Entry>(new Entry(value))); | |
| 53 return 1; | |
| 54 } | 53 } |
| 55 | 54 |
| 56 } // namespace extensions | 55 } // namespace extensions |
| OLD | NEW |