1 | /* |
2 | * Copyright (C) 2011, 2014 Apple Inc. All rights reserved. |
3 | * |
4 | * Redistribution and use in source and binary forms, with or without |
5 | * modification, are permitted provided that the following conditions |
6 | * are met: |
7 | * 1. Redistributions of source code must retain the above copyright |
8 | * notice, this list of conditions and the following disclaimer. |
9 | * 2. Redistributions in binary form must reproduce the above copyright |
10 | * notice, this list of conditions and the following disclaimer in the |
11 | * documentation and/or other materials provided with the distribution. |
12 | * |
13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
24 | */ |
25 | |
26 | #pragma once |
27 | |
28 | #include <wtf/HashMap.h> |
29 | #include <wtf/Vector.h> |
30 | #include <algorithm> |
31 | |
32 | namespace WTF { |
33 | |
34 | template<typename T, typename CounterType = unsigned> |
35 | class Spectrum { |
36 | public: |
37 | typedef typename HashMap<T, CounterType>::iterator iterator; |
38 | typedef typename HashMap<T, CounterType>::const_iterator const_iterator; |
39 | |
40 | Spectrum() { } |
41 | |
42 | void add(const T& key, CounterType count = 1) |
43 | { |
44 | if (!count) |
45 | return; |
46 | typename HashMap<T, CounterType>::AddResult result = m_map.add(key, count); |
47 | if (!result.isNewEntry) |
48 | result.iterator->value += count; |
49 | } |
50 | |
51 | template<typename U> |
52 | void addAll(const Spectrum<T, U>& otherSpectrum) |
53 | { |
54 | for (auto& entry : otherSpectrum) |
55 | add(entry.key, entry.count); |
56 | } |
57 | |
58 | CounterType get(const T& key) const |
59 | { |
60 | const_iterator iter = m_map.find(key); |
61 | if (iter == m_map.end()) |
62 | return 0; |
63 | return iter->value; |
64 | } |
65 | |
66 | size_t size() const { return m_map.size(); } |
67 | |
68 | iterator begin() { return m_map.begin(); } |
69 | iterator end() { return m_map.end(); } |
70 | const_iterator begin() const { return m_map.begin(); } |
71 | const_iterator end() const { return m_map.end(); } |
72 | |
73 | struct KeyAndCount { |
74 | KeyAndCount() { } |
75 | |
76 | KeyAndCount(const T& key, CounterType count) |
77 | : key(key) |
78 | , count(count) |
79 | { |
80 | } |
81 | |
82 | bool operator<(const KeyAndCount& other) const |
83 | { |
84 | if (count != other.count) |
85 | return count < other.count; |
86 | // This causes lower-ordered keys being returned first; this is really just |
87 | // here to make sure that the order is somewhat deterministic rather than being |
88 | // determined by hashing. |
89 | return key > other.key; |
90 | } |
91 | |
92 | T key; |
93 | CounterType count; |
94 | }; |
95 | |
96 | // Returns a list ordered from lowest-count to highest-count. |
97 | Vector<KeyAndCount> buildList() const |
98 | { |
99 | Vector<KeyAndCount> list; |
100 | for (const_iterator iter = begin(); iter != end(); ++iter) |
101 | list.append(KeyAndCount(iter->key, iter->value)); |
102 | |
103 | std::sort(list.begin(), list.end()); |
104 | return list; |
105 | } |
106 | |
107 | void clear() { m_map.clear(); } |
108 | |
109 | template<typename Functor> |
110 | void removeIf(const Functor& functor) |
111 | { |
112 | m_map.removeIf([&functor] (typename HashMap<T, CounterType>::KeyValuePairType& pair) { |
113 | return functor(KeyAndCount(pair.key, pair.value)); |
114 | }); |
115 | } |
116 | |
117 | private: |
118 | HashMap<T, CounterType> m_map; |
119 | }; |
120 | |
121 | } // namespace WTF |
122 | |
123 | using WTF::Spectrum; |
124 | |