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() { | |
mtklein
2014/07/11 17:08:08
Consider this:
SkTHashCache {
this->reset();
}
Rémi Piotaix
2014/07/11 17:45:00
Done.
| |
23 this->clear(); | |
24 } | |
25 | |
26 ~SkTHashCache() { | |
27 fValues.deleteAll(); | |
mtklein
2014/07/11 17:08:08
Looks like we leak the last values?
Rémi Piotaix
2014/07/11 17:45:01
I'm sorry, I don't understand your question.
| |
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 /** | |
39 * If element already in cache, return immediately the cached value | |
40 */ | |
41 T& add(T& add) { | |
mtklein
2014/07/11 17:08:08
Looks like argument to add can be const&?
Rémi Piotaix
2014/07/11 17:45:01
Done.
| |
42 Key key = Traits::GetKey(add); | |
43 if (T * val = this->find(key)) { | |
mtklein
2014/07/11 17:08:08
stray space before *?
Rémi Piotaix
2014/07/11 17:45:01
Done.
| |
44 return *val; | |
45 } | |
46 | |
47 T* element = new T(add); | |
mtklein
2014/07/11 17:08:08
SkNEW
Rémi Piotaix
2014/07/11 17:45:00
Done.
| |
48 | |
49 fDict->add(element); | |
50 | |
51 return *element; | |
52 } | |
53 | |
54 int size() const { | |
55 return fDict->count(); | |
56 } | |
57 | |
58 void clear() { | |
59 if (fDict.get()) { | |
60 typename DictType::Iter it(fDict.get()); | |
61 | |
62 while (!it.done()) { | |
63 delete &(*it); | |
mtklein
2014/07/11 17:08:08
SkDELETE
Rémi Piotaix
2014/07/11 17:45:01
Done.
| |
64 ++it; | |
65 } | |
66 } | |
67 | |
68 fDict.reset(SkNEW(DictType)); | |
69 } | |
70 | |
71 private: | |
72 typedef SkTDynamicHash<T, Key, Traits, kGrowPercent> DictType; | |
73 | |
74 SkAutoTDelete<DictType> fDict; | |
75 SkTDArray<T*> fValues; | |
76 }; | |
77 | |
78 #endif /* SkHASHCACHE_DEFINED */ | |
79 | |
OLD | NEW |