Index: src/core/SkTHashCache.h |
diff --git a/src/core/SkTHashCache.h b/src/core/SkTHashCache.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..b62355a2d448af0d27a4ac33fb86c3f209f68d9c |
--- /dev/null |
+++ b/src/core/SkTHashCache.h |
@@ -0,0 +1,83 @@ |
+/* |
+ * 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() { |
+ this->reset(); |
+ } |
+ |
+ ~SkTHashCache() { |
+ this->clear(); |
+ } |
+ |
+ 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.
|
+ 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(const T& add) { |
+ Key key = Traits::GetKey(add); |
+ if (T* val = this->find(key)) { |
+ return *val; |
+ } |
+ |
+ T* element = SkNEW_ARGS(T, (add)); |
+ |
+ fDict->add(element); |
+ |
+ return *element; |
+ } |
+ |
+ int size() const { |
+ return fDict->count(); |
+ } |
+ |
+ void reset() { |
+ this->clear(); |
+ |
+ fDict.reset(SkNEW(DictType)); |
+ } |
+ |
+private: |
+ typedef SkTDynamicHash<T, Key, Traits, kGrowPercent> DictType; |
+ |
+ void clear() { |
+ if (fDict.get()) { |
+ typename DictType::Iter it(fDict.get()); |
+ |
+ while (!it.done()) { |
+ SkDELETE(&(*it)); |
+ ++it; |
+ } |
+ } |
+ } |
+ |
+ SkAutoTDelete<DictType> fDict; |
+ 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.
|
+}; |
+ |
+#endif /* SkHASHCACHE_DEFINED */ |
+ |