diff options
author | Lorry Tar Creator <lorry-tar-importer@lorry> | 2017-06-27 06:07:23 +0000 |
---|---|---|
committer | Lorry Tar Creator <lorry-tar-importer@lorry> | 2017-06-27 06:07:23 +0000 |
commit | 1bf1084f2b10c3b47fd1a588d85d21ed0eb41d0c (patch) | |
tree | 46dcd36c86e7fbc6e5df36deb463b33e9967a6f7 /Source/WTF/wtf/Spectrum.h | |
parent | 32761a6cee1d0dee366b885b7b9c777e67885688 (diff) | |
download | WebKitGtk-tarball-master.tar.gz |
webkitgtk-2.16.5HEADwebkitgtk-2.16.5master
Diffstat (limited to 'Source/WTF/wtf/Spectrum.h')
-rw-r--r-- | Source/WTF/wtf/Spectrum.h | 41 |
1 files changed, 31 insertions, 10 deletions
diff --git a/Source/WTF/wtf/Spectrum.h b/Source/WTF/wtf/Spectrum.h index 3e6fa4a63..44f7a8180 100644 --- a/Source/WTF/wtf/Spectrum.h +++ b/Source/WTF/wtf/Spectrum.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2011 Apple Inc. All rights reserved. + * Copyright (C) 2011, 2014 Apple Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -32,22 +32,31 @@ namespace WTF { -template<typename T> +template<typename T, typename CounterType = unsigned> class Spectrum { public: - typedef typename HashMap<T, unsigned long>::iterator iterator; - typedef typename HashMap<T, unsigned long>::const_iterator const_iterator; + typedef typename HashMap<T, CounterType>::iterator iterator; + typedef typename HashMap<T, CounterType>::const_iterator const_iterator; Spectrum() { } - void add(const T& key, unsigned long count = 1) + void add(const T& key, CounterType count = 1) { - typename HashMap<T, unsigned long>::AddResult result = m_map.add(key, count); + if (!count) + return; + typename HashMap<T, CounterType>::AddResult result = m_map.add(key, count); if (!result.isNewEntry) result.iterator->value += count; } - unsigned long get(const T& key) const + template<typename U> + void addAll(const Spectrum<T, U>& otherSpectrum) + { + for (auto& entry : otherSpectrum) + add(entry.key, entry.count); + } + + CounterType get(const T& key) const { const_iterator iter = m_map.find(key); if (iter == m_map.end()) @@ -55,6 +64,8 @@ public: return iter->value; } + size_t size() const { return m_map.size(); } + iterator begin() { return m_map.begin(); } iterator end() { return m_map.end(); } const_iterator begin() const { return m_map.begin(); } @@ -63,7 +74,7 @@ public: struct KeyAndCount { KeyAndCount() { } - KeyAndCount(const T& key, unsigned long count) + KeyAndCount(const T& key, CounterType count) : key(key) , count(count) { @@ -80,7 +91,7 @@ public: } T key; - unsigned long count; + CounterType count; }; // Returns a list ordered from lowest-count to highest-count. @@ -94,8 +105,18 @@ public: return list; } + void clear() { m_map.clear(); } + + template<typename Functor> + void removeIf(const Functor& functor) + { + m_map.removeIf([&functor] (typename HashMap<T, CounterType>::KeyValuePairType& pair) { + return functor(KeyAndCount(pair.key, pair.value)); + }); + } + private: - HashMap<T, unsigned long> m_map; + HashMap<T, CounterType> m_map; }; } // namespace WTF |