blob: 156e3adef00263f9265dfeb90a7477ba517bbef0 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
// 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"
#include <algorithm>
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) {
std::swap(*it, entries_.back());
entries_.pop_back();
}
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
|