Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(132)

Unified Diff: src/core/SkTHashCache.h

Issue 364193004: Reopened: Caching the result of readPixelsSupported (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Correcting code as suggested in the comments Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « gyp/core.gypi ('k') | src/gpu/gl/GrGLCaps.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 */
+
« no previous file with comments | « gyp/core.gypi ('k') | src/gpu/gl/GrGLCaps.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698