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 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 84 // Erase the reference to it. This will call the deletor on the removed | 84 // Erase the reference to it. This will call the deletor on the removed |
| 85 // element. The index reference will be replaced in the code below. | 85 // element. The index reference will be replaced in the code below. |
| 86 Erase(index_iter->second); | 86 Erase(index_iter->second); |
| 87 } else if (max_size_ != NO_AUTO_EVICT) { | 87 } else if (max_size_ != NO_AUTO_EVICT) { |
| 88 // New item is being inserted which might make it larger than the maximum | 88 // New item is being inserted which might make it larger than the maximum |
| 89 // size: kick the oldest thing out if necessary. | 89 // size: kick the oldest thing out if necessary. |
| 90 ShrinkToSize(max_size_ - 1); | 90 ShrinkToSize(max_size_ - 1); |
| 91 } | 91 } |
| 92 | 92 |
| 93 ordering_.push_front(value_type(key, payload)); | 93 ordering_.push_front(value_type(key, payload)); |
| 94 index_[key] = ordering_.begin(); | 94 index_.insert(std::make_pair(key, ordering_.begin())); |
|
tzik
2011/06/16 08:55:19
Previous 10 lines guarantee the absence of occupan
| |
| 95 return ordering_.begin(); | 95 return ordering_.begin(); |
| 96 } | 96 } |
| 97 | 97 |
| 98 // Retrieves the contents of the given key, or end() if not found. This method | 98 // Retrieves the contents of the given key, or end() if not found. This method |
| 99 // has the side effect of moving the requested item to the front of the | 99 // has the side effect of moving the requested item to the front of the |
| 100 // recency list. | 100 // recency list. |
| 101 // | 101 // |
| 102 // TODO(brettw) We may want a const version of this function in the future. | 102 // TODO(brettw) We may want a const version of this function in the future. |
| 103 iterator Get(const KeyType& key) { | 103 iterator Get(const KeyType& key) { |
| 104 typename KeyIndex::iterator index_iter = index_.find(key); | 104 typename KeyIndex::iterator index_iter = index_.find(key); |
| (...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 254 virtual ~OwningMRUCache() { | 254 virtual ~OwningMRUCache() { |
| 255 } | 255 } |
| 256 | 256 |
| 257 private: | 257 private: |
| 258 DISALLOW_COPY_AND_ASSIGN(OwningMRUCache); | 258 DISALLOW_COPY_AND_ASSIGN(OwningMRUCache); |
| 259 }; | 259 }; |
| 260 | 260 |
| 261 } // namespace base | 261 } // namespace base |
| 262 | 262 |
| 263 #endif // BASE_MEMORY_MRU_CACHE_H_ | 263 #endif // BASE_MEMORY_MRU_CACHE_H_ |
| OLD | NEW |