[email protected] | c6c301ad | 2014-04-15 18:20:26 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
[email protected] | d9e559d | 2012-07-05 01:04:57 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
3 | // found in the LICENSE file. | ||||
4 | |||||
[email protected] | c6c301ad | 2014-04-15 18:20:26 | [diff] [blame] | 5 | #include "extensions/common/value_counter.h" |
[email protected] | d9e559d | 2012-07-05 01:04:57 | [diff] [blame] | 6 | |
7 | #include <algorithm> | ||||
dcheng | 7c2ca35 | 2016-04-22 21:15:36 | [diff] [blame] | 8 | #include <memory> |
dcheng | e59eca160 | 2015-12-18 17:48:00 | [diff] [blame] | 9 | #include <utility> |
[email protected] | d9e559d | 2012-07-05 01:04:57 | [diff] [blame] | 10 | |
[email protected] | c6c301ad | 2014-04-15 18:20:26 | [diff] [blame] | 11 | #include "base/values.h" |
12 | |||||
[email protected] | d9e559d | 2012-07-05 01:04:57 | [diff] [blame] | 13 | namespace extensions { |
14 | |||||
kalman | bd9f618 | 2015-07-11 00:01:36 | [diff] [blame] | 15 | struct ValueCounter::Entry { |
dcheng | 7c2ca35 | 2016-04-22 21:15:36 | [diff] [blame] | 16 | explicit Entry(std::unique_ptr<base::Value> value) |
dcheng | e59eca160 | 2015-12-18 17:48:00 | [diff] [blame] | 17 | : value(std::move(value)), count(1) {} |
[email protected] | d9e559d | 2012-07-05 01:04:57 | [diff] [blame] | 18 | |
dcheng | 7c2ca35 | 2016-04-22 21:15:36 | [diff] [blame] | 19 | std::unique_ptr<base::Value> value; |
kalman | bd9f618 | 2015-07-11 00:01:36 | [diff] [blame] | 20 | int count; |
21 | }; | ||||
[email protected] | d9e559d | 2012-07-05 01:04:57 | [diff] [blame] | 22 | |
kalman | bd9f618 | 2015-07-11 00:01:36 | [diff] [blame] | 23 | ValueCounter::ValueCounter() { |
24 | } | ||||
[email protected] | d9e559d | 2012-07-05 01:04:57 | [diff] [blame] | 25 | |
kalman | bd9f618 | 2015-07-11 00:01:36 | [diff] [blame] | 26 | ValueCounter::~ValueCounter() { |
27 | } | ||||
[email protected] | d9e559d | 2012-07-05 01:04:57 | [diff] [blame] | 28 | |
kalman | bd9f618 | 2015-07-11 00:01:36 | [diff] [blame] | 29 | bool ValueCounter::Add(const base::Value& value) { |
limasdf | 6ca2e3c | 2015-11-13 22:16:04 | [diff] [blame] | 30 | for (const auto& entry : entries_) { |
kalman | bd9f618 | 2015-07-11 00:01:36 | [diff] [blame] | 31 | if (entry->value->Equals(&value)) { |
32 | ++entry->count; | ||||
33 | return false; | ||||
[email protected] | d9e559d | 2012-07-05 01:04:57 | [diff] [blame] | 34 | } |
35 | } | ||||
Jeremy Roman | 16529d0e | 2017-08-24 18:13:47 | [diff] [blame] | 36 | entries_.push_back(std::make_unique<Entry>(value.CreateDeepCopy())); |
kalman | bd9f618 | 2015-07-11 00:01:36 | [diff] [blame] | 37 | return true; |
[email protected] | d9e559d | 2012-07-05 01:04:57 | [diff] [blame] | 38 | } |
39 | |||||
kalman | bd9f618 | 2015-07-11 00:01:36 | [diff] [blame] | 40 | bool ValueCounter::Remove(const base::Value& value) { |
limasdf | 6ca2e3c | 2015-11-13 22:16:04 | [diff] [blame] | 41 | for (auto it = entries_.begin(); it != entries_.end(); ++it) { |
kalman | bd9f618 | 2015-07-11 00:01:36 | [diff] [blame] | 42 | if ((*it)->value->Equals(&value)) { |
43 | if (--(*it)->count == 0) { | ||||
44 | std::swap(*it, entries_.back()); | ||||
45 | entries_.pop_back(); | ||||
46 | return true; // Removed the last entry. | ||||
47 | } | ||||
48 | return false; // Removed, but no the last entry. | ||||
49 | } | ||||
[email protected] | d9e559d | 2012-07-05 01:04:57 | [diff] [blame] | 50 | } |
kalman | bd9f618 | 2015-07-11 00:01:36 | [diff] [blame] | 51 | return false; // Nothing to remove. |
[email protected] | d9e559d | 2012-07-05 01:04:57 | [diff] [blame] | 52 | } |
53 | |||||
54 | } // namespace extensions |