Chromium Code Reviews| Index: src/core/SkTHashCache.h |
| diff --git a/src/core/SkTHashCache.h b/src/core/SkTHashCache.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..a8b8733993b0080591351b2f9b3f6a02e2c89c53 |
| --- /dev/null |
| +++ b/src/core/SkTHashCache.h |
| @@ -0,0 +1,79 @@ |
| +/* |
| + * Copyright 2014 Google Inc. |
| + * |
| + * Use of this source code is governed by a BSD-style license that can be |
| + * found in the LICENSE file. |
| + */ |
| + |
| +#ifndef SkTHASHCACHE_DEFINED |
| +#define SkTHASHCACHE_DEFINED |
| + |
| +#include "SkTypes.h" |
| +#include "SkTDArray.h" |
| +#include "SkTDynamicHash.h" |
| + |
| +template <typename T, |
| +typename Key, |
| +typename Traits = T, |
| +int kGrowPercent = 75 > |
| +class SkTHashCache : public SkNoncopyable { |
| +public: |
| + |
| + SkTHashCache() { |
|
mtklein
2014/07/11 17:08:08
Consider this:
SkTHashCache {
this->reset();
}
Rémi Piotaix
2014/07/11 17:45:00
Done.
|
| + this->clear(); |
| + } |
| + |
| + ~SkTHashCache() { |
| + 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.
|
| + } |
| + |
| + bool has(const Key& key) const { |
| + return NULL != this->find(key); |
| + } |
| + |
| + T* find(const Key& key) const { |
| + return fDict->find(key); |
| + } |
| + |
| + /** |
| + * If element already in cache, return immediately the cached value |
| + */ |
| + 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.
|
| + Key key = Traits::GetKey(add); |
| + 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.
|
| + return *val; |
| + } |
| + |
| + T* element = new T(add); |
|
mtklein
2014/07/11 17:08:08
SkNEW
Rémi Piotaix
2014/07/11 17:45:00
Done.
|
| + |
| + fDict->add(element); |
| + |
| + return *element; |
| + } |
| + |
| + int size() const { |
| + return fDict->count(); |
| + } |
| + |
| + void clear() { |
| + if (fDict.get()) { |
| + typename DictType::Iter it(fDict.get()); |
| + |
| + while (!it.done()) { |
| + delete &(*it); |
|
mtklein
2014/07/11 17:08:08
SkDELETE
Rémi Piotaix
2014/07/11 17:45:01
Done.
|
| + ++it; |
| + } |
| + } |
| + |
| + fDict.reset(SkNEW(DictType)); |
| + } |
| + |
| +private: |
| + typedef SkTDynamicHash<T, Key, Traits, kGrowPercent> DictType; |
| + |
| + SkAutoTDelete<DictType> fDict; |
| + SkTDArray<T*> fValues; |
| +}; |
| + |
| +#endif /* SkHASHCACHE_DEFINED */ |
| + |