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 | WTF_MAKE_FAST_ALLOCATED; |
37 | public: |
38 | typedef typename HashMap<T, CounterType>::iterator iterator; |
39 | typedef typename HashMap<T, CounterType>::const_iterator const_iterator; |
40 | |
41 | Spectrum() { } |
42 | |
43 | void add(const T& key, CounterType count = 1) |
44 | { |
45 | if (!count) |
46 | return; |
47 | typename HashMap<T, CounterType>::AddResult result = m_map.add(key, count); |
48 | if (!result.isNewEntry) |
49 | result.iterator->value += count; |
50 | } |
51 | |
52 | template<typename U> |
53 | void addAll(const Spectrum<T, U>& otherSpectrum) |
54 | { |
55 | for (auto& entry : otherSpectrum) |
56 | add(entry.key, entry.count); |
57 | } |
58 | |
59 | CounterType get(const T& key) const |
60 | { |
61 | const_iterator iter = m_map.find(key); |
62 | if (iter == m_map.end()) |
63 | return 0; |
64 | return iter->value; |
65 | } |
66 | |
67 | size_t size() const { return m_map.size(); } |
68 | |
69 | iterator begin() { return m_map.begin(); } |
70 | iterator end() { return m_map.end(); } |
71 | const_iterator begin() const { return m_map.begin(); } |
72 | const_iterator end() const { return m_map.end(); } |
73 | |
74 | struct KeyAndCount { |
75 | WTF_MAKE_STRUCT_FAST_ALLOCATED; |
76 | KeyAndCount() { } |
77 | |
78 | KeyAndCount(const T& key, CounterType count) |
79 | : key(key) |
80 | , count(count) |
81 | { |
82 | } |
83 | |
84 | bool operator<(const KeyAndCount& other) const |
85 | { |
86 | if (count != other.count) |
87 | return count < other.count; |
88 | // This causes lower-ordered keys being returned first; this is really just |
89 | // here to make sure that the order is somewhat deterministic rather than being |
90 | // determined by hashing. |
91 | return key > other.key; |
92 | } |
93 | |
94 | T key; |
95 | CounterType count; |
96 | }; |
97 | |
98 | // Returns a list ordered from lowest-count to highest-count. |
99 | Vector<KeyAndCount> buildList() const |
100 | { |
101 | Vector<KeyAndCount> list; |
102 | for (const_iterator iter = begin(); iter != end(); ++iter) |
103 | list.append(KeyAndCount(iter->key, iter->value)); |
104 | |
105 | std::sort(list.begin(), list.end()); |
106 | return list; |
107 | } |
108 | |
109 | void clear() { m_map.clear(); } |
110 | |
111 | template<typename Functor> |
112 | void removeIf(const Functor& functor) |
113 | { |
114 | m_map.removeIf([&functor] (typename HashMap<T, CounterType>::KeyValuePairType& pair) { |
115 | return functor(KeyAndCount(pair.key, pair.value)); |
116 | }); |
117 | } |
118 | |
119 | private: |
120 | HashMap<T, CounterType> m_map; |
121 | }; |
122 | |
123 | } // namespace WTF |
124 | |
125 | using WTF::Spectrum; |
126 | |