summaryrefslogtreecommitdiff
path: root/Source/WTF/wtf/Spectrum.h
diff options
context:
space:
mode:
Diffstat (limited to 'Source/WTF/wtf/Spectrum.h')
-rw-r--r--Source/WTF/wtf/Spectrum.h41
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