Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(364)

Side by Side Diff: base/containers/mru_cache.h

Issue 1515243003: MRUCacheBase - Added Swap method to exchange contents of two MRUCache (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | base/containers/mru_cache_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 119 matching lines...) Expand 10 before | Expand all | Expand 10 after
130 return index_iter->second; 130 return index_iter->second;
131 } 131 }
132 132
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 // Exchanges the contents of |this| by the contents of the |other|.
141 void Swap(MRUCacheBase& other) {
142 ordering_.swap(other.ordering_);
143 index_.swap(other.index_);
144
145 DeletorType temp_deletor(other.deletor_);
Lei Zhang 2015/12/17 23:43:23 Does std::swap() work here and below on line 149?
ramant (doing other things) 2015/12/18 00:32:51 Done.
146 other.deletor_ = deletor_;
147 deletor_ = temp_deletor;
148
149 size_type temp_max_size = other.max_size_;
150 other.max_size_ = max_size_;
151 max_size_ = temp_max_size;
152 }
153
140 // Erases the item referenced by the given iterator. An iterator to the item 154 // Erases the item referenced by the given iterator. An iterator to the item
141 // following it will be returned. The iterator must be valid. 155 // following it will be returned. The iterator must be valid.
142 iterator Erase(iterator pos) { 156 iterator Erase(iterator pos) {
143 deletor_(pos->second); 157 deletor_(pos->second);
144 index_.erase(pos->first); 158 index_.erase(pos->first);
145 return ordering_.erase(pos); 159 return ordering_.erase(pos);
146 } 160 }
147 161
148 // MRUCache entries are often processed in reverse order, so we add this 162 // MRUCache entries are often processed in reverse order, so we add this
149 // convenience function (not typically defined by STL containers). 163 // convenience function (not typically defined by STL containers).
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after
298 virtual ~HashingMRUCache() { 312 virtual ~HashingMRUCache() {
299 } 313 }
300 314
301 private: 315 private:
302 DISALLOW_COPY_AND_ASSIGN(HashingMRUCache); 316 DISALLOW_COPY_AND_ASSIGN(HashingMRUCache);
303 }; 317 };
304 318
305 } // namespace base 319 } // namespace base
306 320
307 #endif // BASE_CONTAINERS_MRU_CACHE_H_ 321 #endif // BASE_CONTAINERS_MRU_CACHE_H_
OLDNEW
« no previous file with comments | « no previous file | base/containers/mru_cache_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698