From 1bf1084f2b10c3b47fd1a588d85d21ed0eb41d0c Mon Sep 17 00:00:00 2001 From: Lorry Tar Creator Date: Tue, 27 Jun 2017 06:07:23 +0000 Subject: webkitgtk-2.16.5 --- Source/WTF/wtf/Spectrum.h | 41 +++++++++++++++++++++++++++++++---------- 1 file changed, 31 insertions(+), 10 deletions(-) (limited to 'Source/WTF/wtf/Spectrum.h') 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 +template class Spectrum { public: - typedef typename HashMap::iterator iterator; - typedef typename HashMap::const_iterator const_iterator; + typedef typename HashMap::iterator iterator; + typedef typename HashMap::const_iterator const_iterator; Spectrum() { } - void add(const T& key, unsigned long count = 1) + void add(const T& key, CounterType count = 1) { - typename HashMap::AddResult result = m_map.add(key, count); + if (!count) + return; + typename HashMap::AddResult result = m_map.add(key, count); if (!result.isNewEntry) result.iterator->value += count; } - unsigned long get(const T& key) const + template + void addAll(const Spectrum& 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 + void removeIf(const Functor& functor) + { + m_map.removeIf([&functor] (typename HashMap::KeyValuePairType& pair) { + return functor(KeyAndCount(pair.key, pair.value)); + }); + } + private: - HashMap m_map; + HashMap m_map; }; } // namespace WTF -- cgit v1.2.1