1 | // -*- mode: c++; c-basic-offset: 4 -*-
|
---|
2 | /*
|
---|
3 | * This file is part of the KDE libraries
|
---|
4 | * Copyright (C) 2005 Apple Computer, Inc.
|
---|
5 | *
|
---|
6 | * This library is free software; you can redistribute it and/or
|
---|
7 | * modify it under the terms of the GNU Library General Public
|
---|
8 | * License as published by the Free Software Foundation; either
|
---|
9 | * version 2 of the License, or (at your option) any later version.
|
---|
10 | *
|
---|
11 | * This library is distributed in the hope that it will be useful,
|
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
14 | * Library General Public License for more details.
|
---|
15 | *
|
---|
16 | * You should have received a copy of the GNU Library General Public License
|
---|
17 | * along with this library; see the file COPYING.LIB. If not, write to
|
---|
18 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
---|
19 | * Boston, MA 02110-1301, USA.
|
---|
20 | *
|
---|
21 | */
|
---|
22 |
|
---|
23 | #ifndef WTF_HashCountedSet_h
|
---|
24 | #define WTF_HashCountedSet_h
|
---|
25 |
|
---|
26 | #include "Assertions.h"
|
---|
27 | #include "HashMap.h"
|
---|
28 | #include "Vector.h"
|
---|
29 |
|
---|
30 | namespace WTF {
|
---|
31 |
|
---|
32 | template<typename Value, typename HashFunctions = typename DefaultHash<Value>::Hash,
|
---|
33 | typename Traits = HashTraits<Value> > class HashCountedSet {
|
---|
34 | private:
|
---|
35 | typedef HashMap<Value, unsigned, HashFunctions, Traits> ImplType;
|
---|
36 | public:
|
---|
37 | typedef Value ValueType;
|
---|
38 | typedef typename ImplType::iterator iterator;
|
---|
39 | typedef typename ImplType::const_iterator const_iterator;
|
---|
40 |
|
---|
41 | HashCountedSet() {}
|
---|
42 |
|
---|
43 | int size() const;
|
---|
44 | int capacity() const;
|
---|
45 | bool isEmpty() const;
|
---|
46 |
|
---|
47 | // iterators iterate over pairs of values and counts
|
---|
48 | iterator begin();
|
---|
49 | iterator end();
|
---|
50 | const_iterator begin() const;
|
---|
51 | const_iterator end() const;
|
---|
52 |
|
---|
53 | iterator find(const ValueType& value);
|
---|
54 | const_iterator find(const ValueType& value) const;
|
---|
55 | bool contains(const ValueType& value) const;
|
---|
56 | unsigned count(const ValueType& value) const;
|
---|
57 |
|
---|
58 | // increases the count if an equal value is already present
|
---|
59 | // the return value is a pair of an interator to the new value's location,
|
---|
60 | // and a bool that is true if an new entry was added
|
---|
61 | std::pair<iterator, bool> add(const ValueType &value);
|
---|
62 |
|
---|
63 | // reduces the count of the value, and removes it if count
|
---|
64 | // goes down to zero
|
---|
65 | void remove(const ValueType& value);
|
---|
66 | void remove(iterator it);
|
---|
67 |
|
---|
68 | void clear();
|
---|
69 |
|
---|
70 | private:
|
---|
71 | ImplType m_impl;
|
---|
72 | };
|
---|
73 |
|
---|
74 | template<typename Value, typename HashFunctions, typename Traits>
|
---|
75 | inline int HashCountedSet<Value, HashFunctions, Traits>::size() const
|
---|
76 | {
|
---|
77 | return m_impl.size();
|
---|
78 | }
|
---|
79 |
|
---|
80 | template<typename Value, typename HashFunctions, typename Traits>
|
---|
81 | inline int HashCountedSet<Value, HashFunctions, Traits>::capacity() const
|
---|
82 | {
|
---|
83 | return m_impl.capacity();
|
---|
84 | }
|
---|
85 |
|
---|
86 | template<typename Value, typename HashFunctions, typename Traits>
|
---|
87 | inline bool HashCountedSet<Value, HashFunctions, Traits>::isEmpty() const
|
---|
88 | {
|
---|
89 | return size() == 0;
|
---|
90 | }
|
---|
91 |
|
---|
92 | template<typename Value, typename HashFunctions, typename Traits>
|
---|
93 | inline typename HashCountedSet<Value, HashFunctions, Traits>::iterator HashCountedSet<Value, HashFunctions, Traits>::begin()
|
---|
94 | {
|
---|
95 | return m_impl.begin();
|
---|
96 | }
|
---|
97 |
|
---|
98 | template<typename Value, typename HashFunctions, typename Traits>
|
---|
99 | inline typename HashCountedSet<Value, HashFunctions, Traits>::iterator HashCountedSet<Value, HashFunctions, Traits>::end()
|
---|
100 | {
|
---|
101 | return m_impl.end();
|
---|
102 | }
|
---|
103 |
|
---|
104 | template<typename Value, typename HashFunctions, typename Traits>
|
---|
105 | inline typename HashCountedSet<Value, HashFunctions, Traits>::const_iterator HashCountedSet<Value, HashFunctions, Traits>::begin() const
|
---|
106 | {
|
---|
107 | return m_impl.begin();
|
---|
108 | }
|
---|
109 |
|
---|
110 | template<typename Value, typename HashFunctions, typename Traits>
|
---|
111 | inline typename HashCountedSet<Value, HashFunctions, Traits>::const_iterator HashCountedSet<Value, HashFunctions, Traits>::end() const
|
---|
112 | {
|
---|
113 | return m_impl.end();
|
---|
114 | }
|
---|
115 |
|
---|
116 | template<typename Value, typename HashFunctions, typename Traits>
|
---|
117 | inline typename HashCountedSet<Value, HashFunctions, Traits>::iterator HashCountedSet<Value, HashFunctions, Traits>::find(const ValueType& value)
|
---|
118 | {
|
---|
119 | return m_impl.find(value);
|
---|
120 | }
|
---|
121 |
|
---|
122 | template<typename Value, typename HashFunctions, typename Traits>
|
---|
123 | inline typename HashCountedSet<Value, HashFunctions, Traits>::const_iterator HashCountedSet<Value, HashFunctions, Traits>::find(const ValueType& value) const
|
---|
124 | {
|
---|
125 | return m_impl.find(value);
|
---|
126 | }
|
---|
127 |
|
---|
128 | template<typename Value, typename HashFunctions, typename Traits>
|
---|
129 | inline bool HashCountedSet<Value, HashFunctions, Traits>::contains(const ValueType& value) const
|
---|
130 | {
|
---|
131 | return m_impl.contains(value);
|
---|
132 | }
|
---|
133 |
|
---|
134 | template<typename Value, typename HashFunctions, typename Traits>
|
---|
135 | inline unsigned HashCountedSet<Value, HashFunctions, Traits>::count(const ValueType& value) const
|
---|
136 | {
|
---|
137 | return m_impl.get(value);
|
---|
138 | }
|
---|
139 |
|
---|
140 | template<typename Value, typename HashFunctions, typename Traits>
|
---|
141 | inline std::pair<typename HashCountedSet<Value, HashFunctions, Traits>::iterator, bool> HashCountedSet<Value, HashFunctions, Traits>::add(const ValueType &value)
|
---|
142 | {
|
---|
143 | pair<iterator, bool> result = m_impl.add(value, 0);
|
---|
144 | ++result.first->second;
|
---|
145 | return result;
|
---|
146 | }
|
---|
147 |
|
---|
148 | template<typename Value, typename HashFunctions, typename Traits>
|
---|
149 | inline void HashCountedSet<Value, HashFunctions, Traits>::remove(const ValueType& value)
|
---|
150 | {
|
---|
151 | remove(find(value));
|
---|
152 | }
|
---|
153 |
|
---|
154 | template<typename Value, typename HashFunctions, typename Traits>
|
---|
155 | inline void HashCountedSet<Value, HashFunctions, Traits>::remove(iterator it)
|
---|
156 | {
|
---|
157 | if (it == end())
|
---|
158 | return;
|
---|
159 |
|
---|
160 | unsigned oldVal = it->second;
|
---|
161 | ASSERT(oldVal != 0);
|
---|
162 | unsigned newVal = oldVal - 1;
|
---|
163 | if (newVal == 0)
|
---|
164 | m_impl.remove(it);
|
---|
165 | else
|
---|
166 | it->second = newVal;
|
---|
167 | }
|
---|
168 |
|
---|
169 | template<typename Value, typename HashFunctions, typename Traits>
|
---|
170 | inline void HashCountedSet<Value, HashFunctions, Traits>::clear()
|
---|
171 | {
|
---|
172 | m_impl.clear();
|
---|
173 | }
|
---|
174 |
|
---|
175 | template<typename Value, typename HashFunctions, typename Traits, typename VectorType>
|
---|
176 | inline void copyToVector(const HashCountedSet<Value, HashFunctions, Traits>& collection, VectorType& vector)
|
---|
177 | {
|
---|
178 | typedef typename HashCountedSet<Value, HashFunctions, Traits>::const_iterator iterator;
|
---|
179 |
|
---|
180 | vector.resize(collection.size());
|
---|
181 |
|
---|
182 | iterator it = collection.begin();
|
---|
183 | iterator end = collection.end();
|
---|
184 | for (unsigned i = 0; it != end; ++it, ++i)
|
---|
185 | vector[i] = *it;
|
---|
186 | }
|
---|
187 |
|
---|
188 | template<typename Value, typename HashFunctions, typename Traits>
|
---|
189 | inline void copyToVector(const HashCountedSet<Value, HashFunctions, Traits>& collection, Vector<Value>& vector)
|
---|
190 | {
|
---|
191 | typedef typename HashCountedSet<Value, HashFunctions, Traits>::const_iterator iterator;
|
---|
192 |
|
---|
193 | vector.resize(collection.size());
|
---|
194 |
|
---|
195 | iterator it = collection.begin();
|
---|
196 | iterator end = collection.end();
|
---|
197 | for (unsigned i = 0; it != end; ++it, ++i)
|
---|
198 | vector[i] = (*it).first;
|
---|
199 | }
|
---|
200 |
|
---|
201 |
|
---|
202 | } // namespace khtml
|
---|
203 |
|
---|
204 | using WTF::HashCountedSet;
|
---|
205 |
|
---|
206 | #endif /* WTF_HashCountedSet_h */
|
---|