Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 /* | |
| 2 * Copyright 2014 Google Inc. | |
| 3 * | |
| 4 * Use of this source code is governed by a BSD-style license that can be | |
| 5 * found in the LICENSE file. | |
| 6 */ | |
| 7 | |
| 8 #ifndef SkTHASHCACHE_DEFINED | |
| 9 #define SkTHASHCACHE_DEFINED | |
| 10 | |
| 11 #include "SkTypes.h" | |
| 12 #include "SkTDArray.h" | |
| 13 #include "SkTDynamicHash.h" | |
| 14 | |
| 15 template <typename T, | |
| 16 typename Key, | |
| 17 typename Traits = T, | |
| 18 int kGrowPercent = 75 > | |
| 19 class SkTHashCache : public SkNoncopyable { | |
| 20 public: | |
| 21 | |
| 22 SkTHashCache() { | |
| 23 this->resetDict(); | |
| 24 } | |
| 25 | |
| 26 ~SkTHashCache() { | |
| 27 fValues.deleteAll(); | |
| 28 } | |
| 29 | |
| 30 bool has(const Key& key) const { | |
| 31 return NULL != this->find(key); | |
| 32 } | |
| 33 | |
| 34 T* find(const Key& key) const { | |
| 35 return fDict->find(key); | |
| 36 } | |
| 37 | |
| 38 // if element already in cache, return immediately the cached value | |
| 39 T& put(T* element) { | |
| 40 Key key = Traits::GetKey(*element); | |
| 41 if (T * val = this->find(key)) { | |
| 42 return *val; | |
| 43 } | |
| 44 | |
| 45 T** elemptr = fValues.append(); | |
| 46 *elemptr = element; | |
| 47 | |
| 48 fDict->add(element); | |
| 49 | |
| 50 return *element; | |
| 51 } | |
| 52 | |
| 53 int size() const { | |
| 54 return fValues.count(); | |
| 55 } | |
| 56 | |
| 57 void clear() { | |
| 58 fValues.deleteAll(); | |
| 59 fValues.reset(); | |
| 60 this->resetDict(); | |
| 61 } | |
| 62 | |
| 63 private: | |
| 64 typedef SkTDynamicHash<T, Key, Traits, kGrowPercent> DictType; | |
| 65 | |
| 66 void resetDict() { | |
| 67 fDict.reset(SkNEW(DictType)); | |
| 68 } | |
| 69 | |
| 70 SkAutoTDelete<DictType> fDict; | |
| 71 SkTDArray<T*> fValues; | |
|
Rémi Piotaix
2014/07/07 18:10:17
Note that the use of an Array here is necessary be
mtklein
2014/07/07 18:33:26
Ah, interesting. Going a step further, is the onl
| |
| 72 }; | |
| 73 | |
| 74 #endif /* SkHASHCACHE_DEFINED */ | |
| 75 | |
| OLD | NEW |