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->reset(); | |
| 24 } | |
| 25 | |
| 26 ~SkTHashCache() { | |
| 27 this->clear(); | |
| 28 } | |
| 29 | |
| 30 bool has(const Key& key) const { | |
|
mtklein
2014/07/11 18:11:38
This appears to be unused except in the test? Mig
Rémi Piotaix
2014/07/14 20:38:59
Done.
| |
| 31 return NULL != this->find(key); | |
| 32 } | |
| 33 | |
| 34 T* find(const Key& key) const { | |
| 35 return fDict->find(key); | |
| 36 } | |
| 37 | |
| 38 /** | |
| 39 * If element already in cache, return immediately the cached value | |
| 40 */ | |
| 41 T& add(const T& add) { | |
| 42 Key key = Traits::GetKey(add); | |
| 43 if (T* val = this->find(key)) { | |
| 44 return *val; | |
| 45 } | |
| 46 | |
| 47 T* element = SkNEW_ARGS(T, (add)); | |
| 48 | |
| 49 fDict->add(element); | |
| 50 | |
| 51 return *element; | |
| 52 } | |
| 53 | |
| 54 int size() const { | |
| 55 return fDict->count(); | |
| 56 } | |
| 57 | |
| 58 void reset() { | |
| 59 this->clear(); | |
| 60 | |
| 61 fDict.reset(SkNEW(DictType)); | |
| 62 } | |
| 63 | |
| 64 private: | |
| 65 typedef SkTDynamicHash<T, Key, Traits, kGrowPercent> DictType; | |
| 66 | |
| 67 void clear() { | |
| 68 if (fDict.get()) { | |
| 69 typename DictType::Iter it(fDict.get()); | |
| 70 | |
| 71 while (!it.done()) { | |
| 72 SkDELETE(&(*it)); | |
| 73 ++it; | |
| 74 } | |
| 75 } | |
| 76 } | |
| 77 | |
| 78 SkAutoTDelete<DictType> fDict; | |
| 79 SkTDArray<T*> fValues; | |
|
mtklein
2014/07/11 18:11:38
So, there was a memory leak before, but you've fix
Rémi Piotaix
2014/07/14 20:38:59
Done.
| |
| 80 }; | |
| 81 | |
| 82 #endif /* SkHASHCACHE_DEFINED */ | |
| 83 | |
| OLD | NEW |