| 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 "SkTDynamicHash.h" | |
| 13 | |
| 14 template <typename T, | |
| 15 typename Key, | |
| 16 typename Traits = T, | |
| 17 int kGrowPercent = 75 > | |
| 18 class SkTHashCache : public SkNoncopyable { | |
| 19 public: | |
| 20 | |
| 21 SkTHashCache() { | |
| 22 this->reset(); | |
| 23 } | |
| 24 | |
| 25 ~SkTHashCache() { | |
| 26 this->clear(); | |
| 27 } | |
| 28 | |
| 29 T* find(const Key& key) const { | |
| 30 return fDict->find(key); | |
| 31 } | |
| 32 | |
| 33 /** | |
| 34 * If element already in cache, return immediately the cached value | |
| 35 */ | |
| 36 T& add(const T& add) { | |
| 37 Key key = Traits::GetKey(add); | |
| 38 if (T* val = this->find(key)) { | |
| 39 return *val; | |
| 40 } | |
| 41 | |
| 42 T* element = SkNEW_ARGS(T, (add)); | |
| 43 | |
| 44 fDict->add(element); | |
| 45 | |
| 46 return *element; | |
| 47 } | |
| 48 | |
| 49 int size() const { | |
| 50 return fDict->count(); | |
| 51 } | |
| 52 | |
| 53 void reset() { | |
| 54 this->clear(); | |
| 55 | |
| 56 fDict.reset(SkNEW(DictType)); | |
| 57 } | |
| 58 | |
| 59 private: | |
| 60 typedef SkTDynamicHash<T, Key, Traits, kGrowPercent> DictType; | |
| 61 | |
| 62 void clear() { | |
| 63 if (fDict.get()) { | |
| 64 typename DictType::Iter it(fDict.get()); | |
| 65 | |
| 66 while (!it.done()) { | |
| 67 SkDELETE(&(*it)); | |
| 68 ++it; | |
| 69 } | |
| 70 } | |
| 71 } | |
| 72 | |
| 73 SkAutoTDelete<DictType> fDict; | |
| 74 }; | |
| 75 | |
| 76 #endif /* SkHASHCACHE_DEFINED */ | |
| 77 | |
| OLD | NEW |