From 55bd940446506f8409f38271f2e4969e9b4f3991 Mon Sep 17 00:00:00 2001 From: "commit-bot@chromium.org" Date: Wed, 2 Apr 2014 19:17:00 +0000 Subject: [PATCH] SkTDynamicHash: pick up GetKey(), Hash() from T by default. This also has a somewhat obscure technical benefit: it removes the requirement that GetKey() and Hash() must be functions with external linkage, which is required when passing a function pointer to a template. A future CL that's run into this problem and the obvious simplification are about 50/50 why I'm sending this CL. BUG=skia: DIFFBASE= https://codereview.chromium.org/222343002/ R=bsalomon@google.com, mtklein@google.com Author: mtklein@chromium.org Review URL: https://codereview.chromium.org/222473002 git-svn-id: http://skia.googlecode.com/svn/trunk@14028 2bbb7eff-a529-9590-31e7-b0007b416f81 --- src/core/SkPictureFlat.h | 9 +++++---- src/core/SkScaledImageCache.cpp | 20 +++++--------------- src/core/SkTDynamicHash.h | 10 ++++++++-- src/gpu/GrResourceCache.h | 5 +---- src/gpu/GrTMultiMap.h | 12 ++++-------- tests/DynamicHashTest.cpp | 9 +++++---- 6 files changed, 28 insertions(+), 37 deletions(-) diff --git a/src/core/SkPictureFlat.h b/src/core/SkPictureFlat.h index f63b9c10a7..4928114660 100644 --- a/src/core/SkPictureFlat.h +++ b/src/core/SkPictureFlat.h @@ -335,9 +335,10 @@ public: } private: - // For SkTDynamicHash. - static const SkFlatData& Identity(const SkFlatData& flat) { return flat; } - static uint32_t Hash(const SkFlatData& flat) { return flat.checksum(); } + struct HashTraits { + static const SkFlatData& GetKey(const SkFlatData& flat) { return flat; } + static uint32_t Hash(const SkFlatData& flat) { return flat.checksum(); } + }; void setIndex(int index) { fIndex = index; } uint8_t* data() { return (uint8_t*)this + sizeof(*this); } @@ -563,7 +564,7 @@ private: SkTDArray fIndexedData; // For SkFlatData -> cached SkFlatData, which has index(). - SkTDynamicHash fHash; + SkTDynamicHash fHash; }; typedef SkFlatDictionary SkPaintDictionary; diff --git a/src/core/SkScaledImageCache.cpp b/src/core/SkScaledImageCache.cpp index 8320b92181..1e37884c31 100644 --- a/src/core/SkScaledImageCache.cpp +++ b/src/core/SkScaledImageCache.cpp @@ -116,6 +116,9 @@ struct SkScaledImageCache::Rec { SkSafeUnref(fMip); } + static const Key& GetKey(const Rec& rec) { return rec.fKey; } + static uint32_t Hash(const Key& key) { return key.fHash; } + size_t bytesUsed() const { return fMip ? fMip->getSize() : fBitmap.getSize(); } @@ -135,21 +138,8 @@ struct SkScaledImageCache::Rec { #include "SkTDynamicHash.h" -namespace { // can't use static functions w/ template parameters -const SkScaledImageCache::Key& key_from_rec(const SkScaledImageCache::Rec& rec) { - return rec.fKey; -} - -uint32_t hash_from_key(const SkScaledImageCache::Key& key) { - return key.fHash; -} - -} // namespace - -class SkScaledImageCache::Hash : public SkTDynamicHash {}; +class SkScaledImageCache::Hash : + public SkTDynamicHash {}; /////////////////////////////////////////////////////////////////////////////// diff --git a/src/core/SkTDynamicHash.h b/src/core/SkTDynamicHash.h index 0e34270e0b..c9a0b3ed01 100644 --- a/src/core/SkTDynamicHash.h +++ b/src/core/SkTDynamicHash.h @@ -12,10 +12,13 @@ #include "SkTemplates.h" #include "SkTypes.h" +// Traits requires: +// static const Key& GetKey(const T&) { ... } +// static uint32_t Hash(const Key&) { ... } +// We'll look on T for these by default, or you can pass a custom Traits type. template // Larger -> more memory efficient, but slower. class SkTDynamicHash { public: @@ -227,6 +230,9 @@ private: return (index + round + 1) & this->hashMask(); } + static const Key& GetKey(const T& t) { return Traits::GetKey(t); } + static uint32_t Hash(const Key& key) { return Traits::Hash(key); } + int fCount; // Number of non Empty(), non Deleted() entries in fArray. int fDeleted; // Number of Deleted() entries in fArray. int fCapacity; // Number of entries in fArray. Always a power of 2. diff --git a/src/gpu/GrResourceCache.h b/src/gpu/GrResourceCache.h index 26423ddc3d..a8309188ff 100644 --- a/src/gpu/GrResourceCache.h +++ b/src/gpu/GrResourceCache.h @@ -315,10 +315,7 @@ private: void removeInvalidResource(GrResourceEntry* entry); - GrTMultiMap fCache; + GrTMultiMap fCache; // We're an internal doubly linked list typedef SkTInternalLList EntryList; diff --git a/src/gpu/GrTMultiMap.h b/src/gpu/GrTMultiMap.h index dfa7e5ec7d..0007a04a2a 100644 --- a/src/gpu/GrTMultiMap.h +++ b/src/gpu/GrTMultiMap.h @@ -17,14 +17,13 @@ */ template + typename HashTraits=T> class GrTMultiMap { struct ValueList { explicit ValueList(T* value) : fValue(value), fNext(NULL) {} - static const Key& ListGetKey(const ValueList& e) { return GetKey(*e.fValue); } - static uint32_t ListHash(const Key& key) { return Hash(key); } + static const Key& GetKey(const ValueList& e) { return HashTraits::GetKey(*e.fValue); } + static uint32_t Hash(const Key& key) { return HashTraits::Hash(key); } T* fValue; ValueList* fNext; }; @@ -104,10 +103,7 @@ public: int count() const { return fCount; } private: - SkTDynamicHash fHash; + SkTDynamicHash fHash; int fCount; }; diff --git a/tests/DynamicHashTest.cpp b/tests/DynamicHashTest.cpp index bb9367b46d..b2da6f3888 100644 --- a/tests/DynamicHashTest.cpp +++ b/tests/DynamicHashTest.cpp @@ -13,12 +13,13 @@ namespace { struct Entry { int key; double value; + + static const int& GetKey(const Entry& entry) { return entry.key; } + static uint32_t Hash(const int& key) { return key; } }; -const int& GetKey(const Entry& entry) { return entry.key; } -uint32_t GetHash(const int& key) { return key; } -class Hash : public SkTDynamicHash { +class Hash : public SkTDynamicHash { public: Hash() : INHERITED() {} @@ -27,7 +28,7 @@ public: int countCollisions(const int& key) const { return this->INHERITED::countCollisions(key); } private: - typedef SkTDynamicHash INHERITED; + typedef SkTDynamicHash INHERITED; }; } // namespace -- 2.34.1