1a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch// Copyright 2014 The Chromium Authors. All rights reserved. 25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file. 45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 5a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch#ifndef EXTENSIONS_COMMON_VALUE_COUNTER_H_ 6a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch#define EXTENSIONS_COMMON_VALUE_COUNTER_H_ 75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <vector> 95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 10a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch#include "base/memory/linked_ptr.h" 11a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch 125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace base { 135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class Value; 145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace extensions { 175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Keeps a running count of Values, like map<Value, int>. Adding / removing 195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// values increments / decrements the count associated with a given Value. 205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Add() and Remove() are linear in the number of Values in the ValueCounter, 225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// because there is no operator<() defined on Value, so we must iterate to find 235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// whether a Value is equal to an existing one. 245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class ValueCounter { 255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public: 265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ValueCounter(); 275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ~ValueCounter(); 285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Adds |value| to the set and returns how many equal values are in the set 305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // after. Does not take ownership of |value|. In the case where a Value equal 315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // to |value| doesn't already exist in this map, this function makes a 325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // DeepCopy() of |value|. 335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int Add(const base::Value& value); 345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Removes |value| from the set and returns how many equal values are in 365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // the set after. 375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int Remove(const base::Value& value); 385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Same as Add() but only performs the add if the value isn't present. 405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int AddIfMissing(const base::Value& value); 415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private: 435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) class Entry { 445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public: 455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) explicit Entry(const base::Value& value); 465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ~Entry(); 475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int Increment(); 495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int Decrement(); 505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const base::Value* value() const { return value_.get(); } 525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int count() const { return count_; } 535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private: 555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) linked_ptr<base::Value> value_; 565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int count_; 575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DISALLOW_COPY_AND_ASSIGN(Entry); 595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) }; 605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) typedef std::vector<linked_ptr<Entry> > EntryList; 615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int AddImpl(const base::Value& value, bool increment); 635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EntryList entries_; 655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DISALLOW_COPY_AND_ASSIGN(ValueCounter); 675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}; 685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} // namespace extensions 705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 71a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch#endif // EXTENSIONS_COMMON_VALUE_COUNTER_H_ 72