diff options
Diffstat (limited to 'chromium/base/stl_util.h')
-rw-r--r-- | chromium/base/stl_util.h | 19 |
1 files changed, 17 insertions, 2 deletions
diff --git a/chromium/base/stl_util.h b/chromium/base/stl_util.h index 3602df0377a..e937d2f3ed9 100644 --- a/chromium/base/stl_util.h +++ b/chromium/base/stl_util.h @@ -90,6 +90,14 @@ void STLDeleteContainerPairSecondPointers(ForwardIterator begin, } } +// Counts the number of instances of val in a container. +template <typename Container, typename T> +typename std::iterator_traits< + typename Container::const_iterator>::difference_type +STLCount(const Container& container, const T& val) { + return std::count(container.begin(), container.end(), val); +} + // To treat a possibly-empty vector as an array, use these functions. // If you know the array will never be empty, you can use &*v.begin() // directly, but that is undefined behaviour if |v| is empty. @@ -148,8 +156,7 @@ template <class T> void STLDeleteValues(T* container) { if (!container) return; - for (typename T::iterator i(container->begin()); i != container->end(); ++i) - delete i->second; + STLDeleteContainerPairSecondPointers(container->begin(), container->end()); container->clear(); } @@ -196,6 +203,14 @@ bool ContainsKey(const Collection& collection, const Key& key) { return collection.find(key) != collection.end(); } +// Test to see if a collection like a vector contains a particular value. +// Returns true if the value is in the collection. +template <typename Collection, typename Value> +bool ContainsValue(const Collection& collection, const Value& value) { + return std::find(collection.begin(), collection.end(), value) != + collection.end(); +} + namespace base { // Returns true if the container is sorted. |