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

Side by Side Diff: media/blink/lru.h

Issue 1165903002: Multi reader/writer cache/buffer (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: one more compile fix Created 5 years, 2 months 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
OLDNEW
(Empty)
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef MEDIA_BLINK_LRU_H_
6 #define MEDIA_BLINK_LRU_H_
7
8 #include <list>
9 #include <map>
10
11 namespace media {
12
13 template<typename T>
14 class LRU {
15 public:
16 // Adds |x| to LRU.
17 // |x| must not already be in the LRU.
18 void Insert(const T& x) {
19 DCHECK(!Contains(x));
20 lru_.push_front(x);
21 pos_[x] = lru_.begin();
22 }
23
24 // Removes |x| from LRU.
25 // |x| must be in the LRU.
26 void Remove(const T& x) {
27 DCHECK(Contains(x)) << " x = " << x;
28 lru_.erase(pos_[x]);
29 pos_.erase(x);
30 }
31
32 // Moves |x| to front of LRU. (most recently used)
33 // If |x| is not in LRU, it is added.
34 void Use(const T& x) {
35 if (Contains(x)) Remove(x);
36 Insert(x);
37 }
38
39 bool Empty() const {
40 return lru_.empty();
41 }
42
43 // Returns the Least Recently Used T.
44 T Pop() {
45 DCHECK(!Empty());
46 T ret = lru_.back();
47 lru_.pop_back();
48 pos_.erase(ret);
49 return ret;
50 }
51
52 T Peek() const {
53 DCHECK(!Empty());
54 return lru_.back();
55 }
56
57 bool Contains(const T& x) const {
58 return pos_.find(x) != pos_.end();
59 }
60 private:
61 std::list<T> lru_;
62 std::map<T, typename std::list<T>::iterator> pos_;
63 };
64
65 } // namespace media
66
67 #endif // MEDIA_BLINK_LRU_H
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698