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 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 143 | 143 |
| 144 // MRUCache entries are often processed in reverse order, so we add this | 144 // MRUCache entries are often processed in reverse order, so we add this |
| 145 // convenience function (not typically defined by STL containers). | 145 // convenience function (not typically defined by STL containers). |
| 146 reverse_iterator Erase(reverse_iterator pos) { | 146 reverse_iterator Erase(reverse_iterator pos) { |
| 147 // We have to actually give it the incremented iterator to delete, since | 147 // We have to actually give it the incremented iterator to delete, since |
| 148 // the forward iterator that base() returns is actually one past the item | 148 // the forward iterator that base() returns is actually one past the item |
| 149 // being iterated over. | 149 // being iterated over. |
| 150 return reverse_iterator(Erase((++pos).base())); | 150 return reverse_iterator(Erase((++pos).base())); |
| 151 } | 151 } |
| 152 | 152 |
| 153 // Updates the |max_size_| of the cache to |new_max_size| and evicts entries, | |
| 154 // if necessary, to satisfy the new maximum. | |
| 155 void SetMaxSize(size_t new_max_size) { | |
|
mmenke
2011/12/06 15:50:38
You're going to need a signoff from an owner of ba
James Simonsen
2011/12/06 19:57:49
Yeah, Will's an owner and he's already given LGTM
| |
| 156 if (new_max_size < max_size_) { | |
| 157 ShrinkToSize(new_max_size); | |
| 158 } | |
| 159 max_size_ = new_max_size; | |
| 160 } | |
| 161 | |
| 153 // Shrinks the cache so it only holds |new_size| items. If |new_size| is | 162 // Shrinks the cache so it only holds |new_size| items. If |new_size| is |
| 154 // bigger or equal to the current number of items, this will do nothing. | 163 // bigger or equal to the current number of items, this will do nothing. |
| 155 void ShrinkToSize(size_type new_size) { | 164 void ShrinkToSize(size_type new_size) { |
| 156 for (size_type i = size(); i > new_size; i--) | 165 for (size_type i = size(); i > new_size; i--) |
| 157 Erase(rbegin()); | 166 Erase(rbegin()); |
| 158 } | 167 } |
| 159 | 168 |
| 160 // Deletes everything from the cache. | 169 // Deletes everything from the cache. |
| 161 void Clear() { | 170 void Clear() { |
| 162 for (typename PayloadList::iterator i(ordering_.begin()); | 171 for (typename PayloadList::iterator i(ordering_.begin()); |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 174 return index_.size(); | 183 return index_.size(); |
| 175 } | 184 } |
| 176 | 185 |
| 177 // Allows iteration over the list. Forward iteration starts with the most | 186 // Allows iteration over the list. Forward iteration starts with the most |
| 178 // recent item and works backwards. | 187 // recent item and works backwards. |
| 179 // | 188 // |
| 180 // Note that since these iterators are actually iterators over a list, you | 189 // Note that since these iterators are actually iterators over a list, you |
| 181 // can keep them as you insert or delete things (as long as you don't delete | 190 // can keep them as you insert or delete things (as long as you don't delete |
| 182 // the one you are pointing to) and they will still be valid. | 191 // the one you are pointing to) and they will still be valid. |
| 183 iterator begin() { return ordering_.begin(); } | 192 iterator begin() { return ordering_.begin(); } |
| 184 const_iterator begin() const { ordering_.begin(); } | 193 const_iterator begin() const { return ordering_.begin(); } |
| 185 iterator end() { return ordering_.end(); } | 194 iterator end() { return ordering_.end(); } |
| 186 const_iterator end() const { return ordering_.end(); } | 195 const_iterator end() const { return ordering_.end(); } |
| 187 | 196 |
| 188 reverse_iterator rbegin() { return ordering_.rbegin(); } | 197 reverse_iterator rbegin() { return ordering_.rbegin(); } |
| 189 const_reverse_iterator rbegin() const { ordering_.rbegin(); } | 198 const_reverse_iterator rbegin() const { return ordering_.rbegin(); } |
| 190 reverse_iterator rend() { return ordering_.rend(); } | 199 reverse_iterator rend() { return ordering_.rend(); } |
| 191 const_reverse_iterator rend() const { return ordering_.rend(); } | 200 const_reverse_iterator rend() const { return ordering_.rend(); } |
| 192 | 201 |
| 193 bool empty() const { return ordering_.empty(); } | 202 bool empty() const { return ordering_.empty(); } |
| 194 | 203 |
| 195 private: | 204 private: |
| 196 PayloadList ordering_; | 205 PayloadList ordering_; |
| 197 KeyIndex index_; | 206 KeyIndex index_; |
| 198 | 207 |
| 199 size_type max_size_; | 208 size_type max_size_; |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 297 virtual ~HashingMRUCache() { | 306 virtual ~HashingMRUCache() { |
| 298 } | 307 } |
| 299 | 308 |
| 300 private: | 309 private: |
| 301 DISALLOW_COPY_AND_ASSIGN(HashingMRUCache); | 310 DISALLOW_COPY_AND_ASSIGN(HashingMRUCache); |
| 302 }; | 311 }; |
| 303 | 312 |
| 304 } // namespace base | 313 } // namespace base |
| 305 | 314 |
| 306 #endif // BASE_MEMORY_MRU_CACHE_H_ | 315 #endif // BASE_MEMORY_MRU_CACHE_H_ |
| OLD | NEW |