| 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 17 matching lines...) Expand all Loading... |
| 28 | 28 |
| 29 // Clear the table. | 29 // Clear the table. |
| 30 void reset() { | 30 void reset() { |
| 31 this->~SkTHashTable(); | 31 this->~SkTHashTable(); |
| 32 SkNEW_PLACEMENT(this, SkTHashTable); | 32 SkNEW_PLACEMENT(this, SkTHashTable); |
| 33 } | 33 } |
| 34 | 34 |
| 35 // How many entries are in the table? | 35 // How many entries are in the table? |
| 36 int count() const { return fCount; } | 36 int count() const { return fCount; } |
| 37 | 37 |
| 38 // Approximately how many bytes of memory do we use beyond sizeof(*this)? |
| 39 size_t approxBytesUsed() const { return fCapacity * sizeof(Slot); } |
| 40 |
| 38 // !!!!!!!!!!!!!!!!! CAUTION !!!!!!!!!!!!!
!!!! | 41 // !!!!!!!!!!!!!!!!! CAUTION !!!!!!!!!!!!!
!!!! |
| 39 // set(), find() and foreach() all allow mutable access to table entries. | 42 // set(), find() and foreach() all allow mutable access to table entries. |
| 40 // If you change an entry so that it no longer has the same key, all hell | 43 // If you change an entry so that it no longer has the same key, all hell |
| 41 // will break loose. Do not do that! | 44 // will break loose. Do not do that! |
| 42 // | 45 // |
| 43 // Please prefer to use SkTHashMap or SkTHashSet, which do not have this dan
ger. | 46 // Please prefer to use SkTHashMap or SkTHashSet, which do not have this dan
ger. |
| 44 | 47 |
| 45 // The pointers returned by set() and find() are valid only until the next c
all to set(). | 48 // The pointers returned by set() and find() are valid only until the next c
all to set(). |
| 46 // The pointers you receive in foreach() are only valid for its duration. | 49 // The pointers you receive in foreach() are only valid for its duration. |
| 47 | 50 |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 192 class SkTHashMap : SkNoncopyable { | 195 class SkTHashMap : SkNoncopyable { |
| 193 public: | 196 public: |
| 194 SkTHashMap() {} | 197 SkTHashMap() {} |
| 195 | 198 |
| 196 // Clear the map. | 199 // Clear the map. |
| 197 void reset() { fTable.reset(); } | 200 void reset() { fTable.reset(); } |
| 198 | 201 |
| 199 // How many key/value pairs are in the table? | 202 // How many key/value pairs are in the table? |
| 200 int count() const { return fTable.count(); } | 203 int count() const { return fTable.count(); } |
| 201 | 204 |
| 205 // Approximately how many bytes of memory do we use beyond sizeof(*this)? |
| 206 size_t approxBytesUsed() const { return fTable.approxBytesUsed(); } |
| 207 |
| 202 // N.B. The pointers returned by set() and find() are valid only until the n
ext call to set(). | 208 // N.B. The pointers returned by set() and find() are valid only until the n
ext call to set(). |
| 203 | 209 |
| 204 // Set key to val in the table, replacing any previous value with the same k
ey. | 210 // Set key to val in the table, replacing any previous value with the same k
ey. |
| 205 // We copy both key and val, and return a pointer to the value copy now in t
he table. | 211 // We copy both key and val, and return a pointer to the value copy now in t
he table. |
| 206 V* set(const K& key, const V& val) { | 212 V* set(const K& key, const V& val) { |
| 207 Pair in = { key, val }; | 213 Pair in = { key, val }; |
| 208 Pair* out = fTable.set(in); | 214 Pair* out = fTable.set(in); |
| 209 return &out->val; | 215 return &out->val; |
| 210 } | 216 } |
| 211 | 217 |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 252 class SkTHashSet : SkNoncopyable { | 258 class SkTHashSet : SkNoncopyable { |
| 253 public: | 259 public: |
| 254 SkTHashSet() {} | 260 SkTHashSet() {} |
| 255 | 261 |
| 256 // Clear the set. | 262 // Clear the set. |
| 257 void reset() { fTable.reset(); } | 263 void reset() { fTable.reset(); } |
| 258 | 264 |
| 259 // How many items are in the set? | 265 // How many items are in the set? |
| 260 int count() const { return fTable.count(); } | 266 int count() const { return fTable.count(); } |
| 261 | 267 |
| 268 // Approximately how many bytes of memory do we use beyond sizeof(*this)? |
| 269 size_t approxBytesUsed() const { return fTable.approxBytesUsed(); } |
| 270 |
| 262 // Copy an item into the set. | 271 // Copy an item into the set. |
| 263 void add(const T& item) { fTable.set(item); } | 272 void add(const T& item) { fTable.set(item); } |
| 264 | 273 |
| 265 // Is this item in the set? | 274 // Is this item in the set? |
| 266 bool contains(const T& item) const { return SkToBool(this->find(item)); } | 275 bool contains(const T& item) const { return SkToBool(this->find(item)); } |
| 267 | 276 |
| 268 // If an item equal to this is in the set, return a pointer to it, otherwise
null. | 277 // If an item equal to this is in the set, return a pointer to it, otherwise
null. |
| 269 // This pointer remains valid until the next call to add(). | 278 // This pointer remains valid until the next call to add(). |
| 270 const T* find(const T& item) const { return fTable.find(item); } | 279 const T* find(const T& item) const { return fTable.find(item); } |
| 271 | 280 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 283 | 292 |
| 284 private: | 293 private: |
| 285 struct Traits { | 294 struct Traits { |
| 286 static const T& GetKey(const T& item) { return item; } | 295 static const T& GetKey(const T& item) { return item; } |
| 287 static uint32_t Hash(const T& item) { return HashT(item); } | 296 static uint32_t Hash(const T& item) { return HashT(item); } |
| 288 }; | 297 }; |
| 289 SkTHashTable<T, T, Traits> fTable; | 298 SkTHashTable<T, T, Traits> fTable; |
| 290 }; | 299 }; |
| 291 | 300 |
| 292 #endif//SkTHash_DEFINED | 301 #endif//SkTHash_DEFINED |
| OLD | NEW |