Index: src/core/SkTHashCache.h |
diff --git a/src/core/SkTHashCache.h b/src/core/SkTHashCache.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..b8282e943372b42d6de79fba75757808e6b348c0 |
--- /dev/null |
+++ b/src/core/SkTHashCache.h |
@@ -0,0 +1,75 @@ |
+/* |
+ * 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->resetDict(); |
+ } |
+ |
+ ~SkTHashCache() { |
+ fValues.deleteAll(); |
+ } |
+ |
+ 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& put(T* element) { |
+ Key key = Traits::GetKey(*element); |
+ if (T * val = this->find(key)) { |
+ return *val; |
+ } |
+ |
+ T** elemptr = fValues.append(); |
+ *elemptr = element; |
+ |
+ fDict->add(element); |
+ |
+ return *element; |
+ } |
+ |
+ int size() const { |
+ return fValues.count(); |
+ } |
+ |
+ void clear() { |
+ fValues.deleteAll(); |
+ fValues.reset(); |
+ this->resetDict(); |
+ } |
+ |
+private: |
+ typedef SkTDynamicHash<T, Key, Traits, kGrowPercent> DictType; |
+ |
+ void resetDict() { |
+ fDict.reset(SkNEW(DictType)); |
+ } |
+ |
+ SkAutoTDelete<DictType> fDict; |
+ SkTDArray<T*> fValues; |
Rémi Piotaix
2014/07/07 18:10:17
Note that the use of an Array here is necessary be
mtklein
2014/07/07 18:33:26
Ah, interesting. Going a step further, is the onl
|
+}; |
+ |
+#endif /* SkHASHCACHE_DEFINED */ |
+ |