OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // This file contains a template for a Most Recently Used cache that allows | 5 // This file contains a template for a Most Recently Used cache that allows |
6 // constant-time access to items using a key, but easy identification of the | 6 // constant-time access to items using a key, but easy identification of the |
7 // least-recently-used items for removal. Each key can only be associated with | 7 // least-recently-used items for removal. Each key can only be associated with |
8 // one payload item at a time. | 8 // one payload item at a time. |
9 // | 9 // |
10 // The key object will be stored twice, so it should support efficient copying. | 10 // The key object will be stored twice, so it should support efficient copying. |
11 // | 11 // |
12 // NOTE: While all operations are O(1), this code is written for | 12 // NOTE: While all operations are O(1), this code is written for |
13 // legibility rather than optimality. If future profiling identifies this as | 13 // legibility rather than optimality. If future profiling identifies this as |
14 // a bottleneck, there is room for smaller values of 1 in the O(1). :] | 14 // a bottleneck, there is room for smaller values of 1 in the O(1). :] |
15 | 15 |
16 #ifndef BASE_MEMORY_MRU_CACHE_H_ | 16 #ifndef BASE_MEMORY_MRU_CACHE_H_ |
17 #define BASE_MEMORY_MRU_CACHE_H_ | 17 #define BASE_MEMORY_MRU_CACHE_H_ |
18 #pragma once | 18 #pragma once |
19 | 19 |
20 #include <list> | 20 #include <list> |
21 #include <map> | 21 #include <map> |
22 #include <utility> | 22 #include <utility> |
23 | 23 |
24 #include "base/basictypes.h" | 24 #include "base/basictypes.h" |
| 25 #include "base/hash_tables.h" |
25 #include "base/logging.h" | 26 #include "base/logging.h" |
26 | 27 |
27 namespace base { | 28 namespace base { |
28 | 29 |
29 // MRUCacheBase ---------------------------------------------------------------- | 30 // MRUCacheBase ---------------------------------------------------------------- |
30 | 31 |
| 32 // This template is used to standardize map type containers that can be used |
| 33 // by MRUCacheBase. This level of indirection is necessary because of the way |
| 34 // that template template params and default template params interact. |
| 35 template <class KeyType, class ValueType> |
| 36 struct MRUCacheStandardMap { |
| 37 typedef std::map<KeyType, ValueType> Type; |
| 38 }; |
| 39 |
31 // Base class for the MRU cache specializations defined below. | 40 // Base class for the MRU cache specializations defined below. |
32 // The deletor will get called on all payloads that are being removed or | 41 // The deletor will get called on all payloads that are being removed or |
33 // replaced. | 42 // replaced. |
34 template <class KeyType, class PayloadType, class DeletorType> | 43 template <class KeyType, class PayloadType, class DeletorType, |
| 44 template <typename, typename> class MapType = MRUCacheStandardMap> |
35 class MRUCacheBase { | 45 class MRUCacheBase { |
36 public: | 46 public: |
37 // The payload of the list. This maintains a copy of the key so we can | 47 // The payload of the list. This maintains a copy of the key so we can |
38 // efficiently delete things given an element of the list. | 48 // efficiently delete things given an element of the list. |
39 typedef std::pair<KeyType, PayloadType> value_type; | 49 typedef std::pair<KeyType, PayloadType> value_type; |
40 | 50 |
41 private: | 51 private: |
42 typedef std::list<value_type> PayloadList; | 52 typedef std::list<value_type> PayloadList; |
43 typedef std::map<KeyType, typename PayloadList::iterator> KeyIndex; | 53 typedef typename MapType<KeyType, |
| 54 typename PayloadList::iterator>::Type KeyIndex; |
44 | 55 |
45 public: | 56 public: |
46 typedef typename PayloadList::size_type size_type; | 57 typedef typename PayloadList::size_type size_type; |
47 | 58 |
48 typedef typename PayloadList::iterator iterator; | 59 typedef typename PayloadList::iterator iterator; |
49 typedef typename PayloadList::const_iterator const_iterator; | 60 typedef typename PayloadList::const_iterator const_iterator; |
50 typedef typename PayloadList::reverse_iterator reverse_iterator; | 61 typedef typename PayloadList::reverse_iterator reverse_iterator; |
51 typedef typename PayloadList::const_reverse_iterator const_reverse_iterator; | 62 typedef typename PayloadList::const_reverse_iterator const_reverse_iterator; |
52 | 63 |
53 enum { NO_AUTO_EVICT = 0 }; | 64 enum { NO_AUTO_EVICT = 0 }; |
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
251 explicit OwningMRUCache(typename ParentType::size_type max_size) | 262 explicit OwningMRUCache(typename ParentType::size_type max_size) |
252 : ParentType(max_size) { | 263 : ParentType(max_size) { |
253 } | 264 } |
254 virtual ~OwningMRUCache() { | 265 virtual ~OwningMRUCache() { |
255 } | 266 } |
256 | 267 |
257 private: | 268 private: |
258 DISALLOW_COPY_AND_ASSIGN(OwningMRUCache); | 269 DISALLOW_COPY_AND_ASSIGN(OwningMRUCache); |
259 }; | 270 }; |
260 | 271 |
| 272 // HashingMRUCache ------------------------------------------------------------ |
| 273 |
| 274 template <class KeyType, class ValueType> |
| 275 struct MRUCacheHashMap { |
| 276 typedef base::hash_map<KeyType, ValueType> Type; |
| 277 }; |
| 278 |
| 279 // This class is similar to MRUCache, except that it uses base::hash_map as |
| 280 // the map type instead of std::map. Note that your KeyType must be hashable |
| 281 // to use this cache. |
| 282 template <class KeyType, class PayloadType> |
| 283 class HashingMRUCache : public MRUCacheBase<KeyType, |
| 284 PayloadType, |
| 285 MRUCacheNullDeletor<PayloadType>, |
| 286 MRUCacheHashMap> { |
| 287 private: |
| 288 typedef MRUCacheBase<KeyType, PayloadType, |
| 289 MRUCacheNullDeletor<PayloadType>, |
| 290 MRUCacheHashMap> ParentType; |
| 291 |
| 292 public: |
| 293 // See MRUCacheBase, noting the possibility of using NO_AUTO_EVICT. |
| 294 explicit HashingMRUCache(typename ParentType::size_type max_size) |
| 295 : ParentType(max_size) { |
| 296 } |
| 297 virtual ~HashingMRUCache() { |
| 298 } |
| 299 |
| 300 private: |
| 301 DISALLOW_COPY_AND_ASSIGN(HashingMRUCache); |
| 302 }; |
| 303 |
261 } // namespace base | 304 } // namespace base |
262 | 305 |
263 #endif // BASE_MEMORY_MRU_CACHE_H_ | 306 #endif // BASE_MEMORY_MRU_CACHE_H_ |
OLD | NEW |