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

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

Issue 1427433012: Simple LRU class (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: renamed kTestSize Created 5 years, 1 month 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 | « media/blink/BUILD.gn ('k') | media/blink/lru_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
(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
10 #include "base/containers/hash_tables.h"
11 #include "base/macros.h"
12
13 namespace media {
14
15 // Simple LRU (least recently used) class.
16 // Keeps track of a set of data and lets you get the least recently used
17 // (oldest) element at any time. All operations are O(1). Elements are expected
18 // to be hashable and unique.
19 // Example:
20 // LRU<int> lru;
21 // lru.Insert(1);
22 // lru.Insert(2);
23 // lru.Insert(3);
24 // lru.Use(1);
25 // cout << lru.Pop(); // this will print "2"
26 template <typename T>
27 class LRU {
28 public:
29 LRU() {}
30
31 // Adds |x| to LRU.
32 // |x| must not already be in the LRU.
33 // Faster than Use(), and will DCHECK that |x| is not in the LRU.
34 void Insert(const T& x) {
35 DCHECK(!Contains(x));
36 lru_.push_front(x);
37 pos_[x] = lru_.begin();
38 }
39
40 // Removes |x| from LRU.
41 // |x| must be in the LRU.
42 void Remove(const T& x) {
43 DCHECK(Contains(x));
44 lru_.erase(pos_[x]);
45 pos_.erase(x);
46 }
47
48 // Moves |x| to front of LRU. (most recently used)
49 // If |x| is not in LRU, it is added.
50 // Please call Insert() if you know that |x| is not in the LRU.
51 void Use(const T& x) {
52 if (Contains(x))
53 Remove(x);
54 Insert(x);
55 }
56
57 bool Empty() const { return lru_.empty(); }
58
59 // Returns the Least Recently Used T and removes it.
60 T Pop() {
61 DCHECK(!Empty());
62 T ret = lru_.back();
63 lru_.pop_back();
64 pos_.erase(ret);
65 return ret;
66 }
67
68 // Returns the Least Recently Used T _without_ removing it.
69 T Peek() const {
70 DCHECK(!Empty());
71 return lru_.back();
72 }
73
74 bool Contains(const T& x) const { return pos_.find(x) != pos_.end(); }
75
76 size_t Size() const { return pos_.size(); }
77
78 private:
79 friend class LRUTest;
80
81 // Linear list of elements, most recently used first.
82 std::list<T> lru_;
83
84 // Maps element values to positions in the list so that we
85 // can quickly remove elements.
86 base::hash_map<T, typename std::list<T>::iterator> pos_;
87
88 DISALLOW_COPY_AND_ASSIGN(LRU);
89 };
90
91 } // namespace media
92
93 #endif // MEDIA_BLINK_LRU_H
OLDNEW
« no previous file with comments | « media/blink/BUILD.gn ('k') | media/blink/lru_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698