Chromium Code Reviews| 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. |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 133 const_iterator Peek(const KeyType& key) const { | 133 const_iterator Peek(const KeyType& key) const { |
| 134 typename KeyIndex::const_iterator index_iter = index_.find(key); | 134 typename KeyIndex::const_iterator index_iter = index_.find(key); |
| 135 if (index_iter == index_.end()) | 135 if (index_iter == index_.end()) |
| 136 return end(); | 136 return end(); |
| 137 return index_iter->second; | 137 return index_iter->second; |
| 138 } | 138 } |
| 139 | 139 |
| 140 // Erases the item referenced by the given iterator. An iterator to the item | 140 // Erases the item referenced by the given iterator. An iterator to the item |
| 141 // following it will be returned. The iterator must be valid. | 141 // following it will be returned. The iterator must be valid. |
| 142 iterator Erase(iterator pos) { | 142 iterator Erase(iterator pos) { |
| 143 deletor_(pos->second); | 143 deletor_(&pos->second); |
| 144 index_.erase(pos->first); | 144 index_.erase(pos->first); |
| 145 return ordering_.erase(pos); | 145 return ordering_.erase(pos); |
| 146 } | 146 } |
| 147 | 147 |
| 148 // MRUCache entries are often processed in reverse order, so we add this | 148 // MRUCache entries are often processed in reverse order, so we add this |
| 149 // convenience function (not typically defined by STL containers). | 149 // convenience function (not typically defined by STL containers). |
| 150 reverse_iterator Erase(reverse_iterator pos) { | 150 reverse_iterator Erase(reverse_iterator pos) { |
| 151 // We have to actually give it the incremented iterator to delete, since | 151 // We have to actually give it the incremented iterator to delete, since |
| 152 // the forward iterator that base() returns is actually one past the item | 152 // the forward iterator that base() returns is actually one past the item |
| 153 // being iterated over. | 153 // being iterated over. |
| 154 return reverse_iterator(Erase((++pos).base())); | 154 return reverse_iterator(Erase((++pos).base())); |
| 155 } | 155 } |
| 156 | 156 |
| 157 // Shrinks the cache so it only holds |new_size| items. If |new_size| is | 157 // Shrinks the cache so it only holds |new_size| items. If |new_size| is |
| 158 // bigger or equal to the current number of items, this will do nothing. | 158 // bigger or equal to the current number of items, this will do nothing. |
| 159 void ShrinkToSize(size_type new_size) { | 159 void ShrinkToSize(size_type new_size) { |
| 160 for (size_type i = size(); i > new_size; i--) | 160 for (size_type i = size(); i > new_size; i--) |
| 161 Erase(rbegin()); | 161 Erase(rbegin()); |
| 162 } | 162 } |
| 163 | 163 |
| 164 // Deletes everything from the cache. | 164 // Deletes everything from the cache. |
| 165 void Clear() { | 165 void Clear() { |
| 166 for (typename PayloadList::iterator i(ordering_.begin()); | 166 for (typename PayloadList::iterator i(ordering_.begin()); |
| 167 i != ordering_.end(); ++i) | 167 i != ordering_.end(); ++i) |
| 168 deletor_(i->second); | 168 deletor_(&i->second); |
| 169 index_.clear(); | 169 index_.clear(); |
| 170 ordering_.clear(); | 170 ordering_.clear(); |
| 171 } | 171 } |
| 172 | 172 |
| 173 // Returns the number of elements in the cache. | 173 // Returns the number of elements in the cache. |
| 174 size_type size() const { | 174 size_type size() const { |
| 175 // We don't use ordering_.size() for the return value because | 175 // We don't use ordering_.size() for the return value because |
| 176 // (as a linked list) it can be O(n). | 176 // (as a linked list) it can be O(n). |
| 177 DCHECK(index_.size() == ordering_.size()); | 177 DCHECK(index_.size() == ordering_.size()); |
| 178 return index_.size(); | 178 return index_.size(); |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 206 | 206 |
| 207 DISALLOW_COPY_AND_ASSIGN(MRUCacheBase); | 207 DISALLOW_COPY_AND_ASSIGN(MRUCacheBase); |
| 208 }; | 208 }; |
| 209 | 209 |
| 210 // MRUCache -------------------------------------------------------------------- | 210 // MRUCache -------------------------------------------------------------------- |
| 211 | 211 |
| 212 // A functor that does nothing. Used by the MRUCache. | 212 // A functor that does nothing. Used by the MRUCache. |
| 213 template<class PayloadType> | 213 template<class PayloadType> |
| 214 class MRUCacheNullDeletor { | 214 class MRUCacheNullDeletor { |
| 215 public: | 215 public: |
| 216 void operator()(PayloadType& payload) { | 216 void operator()(PayloadType* payload) {} |
| 217 } | |
| 218 }; | 217 }; |
| 219 | 218 |
| 220 // A container that does not do anything to free its data. Use this when storing | 219 // A container that does not do anything to free its data. Use this when storing |
| 221 // value types (as opposed to pointers) in the list. | 220 // value types (as opposed to pointers) in the list. |
| 222 template <class KeyType, class PayloadType> | 221 template <class KeyType, class PayloadType> |
| 223 class MRUCache : public MRUCacheBase<KeyType, | 222 class MRUCache : public MRUCacheBase<KeyType, |
| 224 PayloadType, | 223 PayloadType, |
| 225 MRUCacheNullDeletor<PayloadType> > { | 224 MRUCacheNullDeletor<PayloadType> > { |
| 226 private: | 225 private: |
| 227 typedef MRUCacheBase<KeyType, PayloadType, | 226 typedef MRUCacheBase<KeyType, PayloadType, |
| 228 MRUCacheNullDeletor<PayloadType> > ParentType; | 227 MRUCacheNullDeletor<PayloadType> > ParentType; |
| 229 | 228 |
| 230 public: | 229 public: |
| 231 // See MRUCacheBase, noting the possibility of using NO_AUTO_EVICT. | 230 // See MRUCacheBase, noting the possibility of using NO_AUTO_EVICT. |
| 232 explicit MRUCache(typename ParentType::size_type max_size) | 231 explicit MRUCache(typename ParentType::size_type max_size) |
| 233 : ParentType(max_size) { | 232 : ParentType(max_size) { |
| 234 } | 233 } |
| 235 virtual ~MRUCache() { | 234 virtual ~MRUCache() { |
| 236 } | 235 } |
| 237 | 236 |
| 238 private: | 237 private: |
| 239 DISALLOW_COPY_AND_ASSIGN(MRUCache); | 238 DISALLOW_COPY_AND_ASSIGN(MRUCache); |
| 240 }; | 239 }; |
| 241 | 240 |
| 242 // OwningMRUCache -------------------------------------------------------------- | 241 // OwningMRUCache -------------------------------------------------------------- |
| 243 | 242 |
| 244 template<class PayloadType> | 243 template<class PayloadType> |
| 245 class MRUCachePointerDeletor { | 244 class MRUCachePointerDeletor { |
| 246 public: | 245 public: |
| 247 void operator()(PayloadType& payload) { | 246 void operator()(PayloadType* payload) { delete *payload; } |
|
Nico
2015/03/07 01:06:14
can't you just make this const PalyloadType& ? I t
| |
| 248 delete payload; | |
| 249 } | |
| 250 }; | 247 }; |
| 251 | 248 |
| 252 // A cache that owns the payload type, which must be a non-const pointer type. | 249 // A cache that owns the payload type, which must be a non-const pointer type. |
| 253 // The pointers will be deleted when they are removed, replaced, or when the | 250 // The pointers will be deleted when they are removed, replaced, or when the |
| 254 // cache is destroyed. | 251 // cache is destroyed. |
| 255 template <class KeyType, class PayloadType> | 252 template <class KeyType, class PayloadType> |
| 256 class OwningMRUCache | 253 class OwningMRUCache |
| 257 : public MRUCacheBase<KeyType, | 254 : public MRUCacheBase<KeyType, |
| 258 PayloadType, | 255 PayloadType, |
| 259 MRUCachePointerDeletor<PayloadType> > { | 256 MRUCachePointerDeletor<PayloadType> > { |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 301 virtual ~HashingMRUCache() { | 298 virtual ~HashingMRUCache() { |
| 302 } | 299 } |
| 303 | 300 |
| 304 private: | 301 private: |
| 305 DISALLOW_COPY_AND_ASSIGN(HashingMRUCache); | 302 DISALLOW_COPY_AND_ASSIGN(HashingMRUCache); |
| 306 }; | 303 }; |
| 307 | 304 |
| 308 } // namespace base | 305 } // namespace base |
| 309 | 306 |
| 310 #endif // BASE_CONTAINERS_MRU_CACHE_H_ | 307 #endif // BASE_CONTAINERS_MRU_CACHE_H_ |
| OLD | NEW |