Chromium Code Reviews| Index: base/memory/mru_cache.h |
| diff --git a/base/memory/mru_cache.h b/base/memory/mru_cache.h |
| index 32a6fbbbdbe990223dff6cd85780b2f78949fe4d..a744e628ecca8dc8c36f978d58575676aaee9fa4 100644 |
| --- a/base/memory/mru_cache.h |
| +++ b/base/memory/mru_cache.h |
| @@ -23,15 +23,25 @@ |
| #include "base/basictypes.h" |
| #include "base/logging.h" |
| +#include "base/hash_tables.h" |
| namespace base { |
| // MRUCacheBase ---------------------------------------------------------------- |
| +// This template is used to standardize map type containers that can be used |
| +// by MRUCacheBase. This level of indirection is necessary because of the way |
| +// that template template params and default template params interact. |
| +template <class KeyType, class ValueType> |
| +struct StandardMap { |
|
brettw
2011/08/03 20:47:38
How about calling this MRUCacheStandardMap since p
Garrett Casto
2011/08/04 00:03:51
Done. Changed the naming of the HashMap to MRUCach
|
| + typedef std::map<KeyType, ValueType> Type; |
| +}; |
| + |
| // Base class for the MRU cache specializations defined below. |
| // The deletor will get called on all payloads that are being removed or |
| // replaced. |
| -template <class KeyType, class PayloadType, class DeletorType> |
| +template <class KeyType, class PayloadType, class DeletorType, |
| + template <typename, typename> class MapType = StandardMap> |
| class MRUCacheBase { |
| public: |
| // The payload of the list. This maintains a copy of the key so we can |
| @@ -40,7 +50,8 @@ class MRUCacheBase { |
| private: |
| typedef std::list<value_type> PayloadList; |
| - typedef std::map<KeyType, typename PayloadList::iterator> KeyIndex; |
| + typedef typename MapType<KeyType, |
| + typename PayloadList::iterator>::Type KeyIndex; |
| public: |
| typedef typename PayloadList::size_type size_type; |
| @@ -258,6 +269,38 @@ class OwningMRUCache |
| DISALLOW_COPY_AND_ASSIGN(OwningMRUCache); |
| }; |
| +// HashingMRUCache ------------------------------------------------------------ |
| + |
| +template <class KeyType, class ValueType> |
| +struct HashMap { |
| + typedef base::hash_map<KeyType, ValueType> Type; |
| +}; |
| + |
| +// This class is similar to MRUCache, except that it uses base::hash_map as |
| +// the map type instead of std::map. Note that you your KeyType must be hashable |
| +// to use this cache. |
| +template <class KeyType, class PayloadType> |
| +class HashingMRUCache : public MRUCacheBase<KeyType, |
| + PayloadType, |
| + MRUCacheNullDeletor<PayloadType>, |
| + HashMap> { |
| + private: |
| + typedef MRUCacheBase<KeyType, PayloadType, |
| + MRUCacheNullDeletor<PayloadType>, |
| + HashMap> ParentType; |
| + |
| + public: |
| + // See MRUCacheBase, noting the possibility of using NO_AUTO_EVICT. |
| + explicit HashingMRUCache(typename ParentType::size_type max_size) |
| + : ParentType(max_size) { |
| + } |
| + virtual ~HashingMRUCache() { |
| + } |
| + |
| + private: |
| + DISALLOW_COPY_AND_ASSIGN(HashingMRUCache); |
| +}; |
| + |
| } // namespace base |
| #endif // BASE_MEMORY_MRU_CACHE_H_ |