OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #ifndef SkTHash_DEFINED | 8 #ifndef SkTHash_DEFINED |
9 #define SkTHash_DEFINED | 9 #define SkTHash_DEFINED |
10 | 10 |
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
184 T val; | 184 T val; |
185 uint32_t hash; | 185 uint32_t hash; |
186 }; | 186 }; |
187 | 187 |
188 int fCount, fRemoved, fCapacity; | 188 int fCount, fRemoved, fCapacity; |
189 SkAutoTArray<Slot> fSlots; | 189 SkAutoTArray<Slot> fSlots; |
190 }; | 190 }; |
191 | 191 |
192 // Maps K->V. A more user-friendly wrapper around SkTHashTable, suitable for mo
st use cases. | 192 // Maps K->V. A more user-friendly wrapper around SkTHashTable, suitable for mo
st use cases. |
193 // K and V are treated as ordinary copyable C++ types, with no assumed relations
hip between the two. | 193 // K and V are treated as ordinary copyable C++ types, with no assumed relations
hip between the two. |
194 template <typename K, typename V, uint32_t(*HashK)(const K&) = &SkGoodHash> | 194 template <typename K, typename V, typename HashK = SkGoodHash> |
195 class SkTHashMap : SkNoncopyable { | 195 class SkTHashMap : SkNoncopyable { |
196 public: | 196 public: |
197 SkTHashMap() {} | 197 SkTHashMap() {} |
198 | 198 |
199 // Clear the map. | 199 // Clear the map. |
200 void reset() { fTable.reset(); } | 200 void reset() { fTable.reset(); } |
201 | 201 |
202 // How many key/value pairs are in the table? | 202 // How many key/value pairs are in the table? |
203 int count() const { return fTable.count(); } | 203 int count() const { return fTable.count(); } |
204 | 204 |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
240 template <typename Fn> // f(K, V), f(const K&, V), f(K, const V&) or f(cons
t K&, const V&). | 240 template <typename Fn> // f(K, V), f(const K&, V), f(K, const V&) or f(cons
t K&, const V&). |
241 void foreach(Fn&& fn) const { | 241 void foreach(Fn&& fn) const { |
242 fTable.foreach([&fn](const Pair& p){ fn(p.key, p.val); }); | 242 fTable.foreach([&fn](const Pair& p){ fn(p.key, p.val); }); |
243 } | 243 } |
244 | 244 |
245 private: | 245 private: |
246 struct Pair { | 246 struct Pair { |
247 K key; | 247 K key; |
248 V val; | 248 V val; |
249 static const K& GetKey(const Pair& p) { return p.key; } | 249 static const K& GetKey(const Pair& p) { return p.key; } |
250 static uint32_t Hash(const K& key) { return HashK(key); } | 250 static uint32_t Hash(const K& key) { return HashK()(key); } |
251 }; | 251 }; |
252 | 252 |
253 SkTHashTable<Pair, K> fTable; | 253 SkTHashTable<Pair, K> fTable; |
254 }; | 254 }; |
255 | 255 |
256 // A set of T. T is treated as an ordiary copyable C++ type. | 256 // A set of T. T is treated as an ordiary copyable C++ type. |
257 template <typename T, uint32_t(*HashT)(const T&) = &SkGoodHash> | 257 template <typename T, typename HashT = SkGoodHash> |
258 class SkTHashSet : SkNoncopyable { | 258 class SkTHashSet : SkNoncopyable { |
259 public: | 259 public: |
260 SkTHashSet() {} | 260 SkTHashSet() {} |
261 | 261 |
262 // Clear the set. | 262 // Clear the set. |
263 void reset() { fTable.reset(); } | 263 void reset() { fTable.reset(); } |
264 | 264 |
265 // How many items are in the set? | 265 // How many items are in the set? |
266 int count() const { return fTable.count(); } | 266 int count() const { return fTable.count(); } |
267 | 267 |
(...skipping 18 matching lines...) Expand all Loading... |
286 | 286 |
287 // Call fn on every item in the set. You may not mutate anything. | 287 // Call fn on every item in the set. You may not mutate anything. |
288 template <typename Fn> // f(T), f(const T&) | 288 template <typename Fn> // f(T), f(const T&) |
289 void foreach (Fn&& fn) const { | 289 void foreach (Fn&& fn) const { |
290 fTable.foreach(fn); | 290 fTable.foreach(fn); |
291 } | 291 } |
292 | 292 |
293 private: | 293 private: |
294 struct Traits { | 294 struct Traits { |
295 static const T& GetKey(const T& item) { return item; } | 295 static const T& GetKey(const T& item) { return item; } |
296 static uint32_t Hash(const T& item) { return HashT(item); } | 296 static uint32_t Hash(const T& item) { return HashT()(item); } |
297 }; | 297 }; |
298 SkTHashTable<T, T, Traits> fTable; | 298 SkTHashTable<T, T, Traits> fTable; |
299 }; | 299 }; |
300 | 300 |
301 #endif//SkTHash_DEFINED | 301 #endif//SkTHash_DEFINED |
OLD | NEW |