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: media/blink/lru.h

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

Powered by Google App Engine
This is Rietveld 408576698