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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: media/blink/lru.h
diff --git a/media/blink/lru.h b/media/blink/lru.h
new file mode 100644
index 0000000000000000000000000000000000000000..1bd95d6992f9b25c8ab837ddcd8ad0a9bbd4ecf2
--- /dev/null
+++ b/media/blink/lru.h
@@ -0,0 +1,67 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef MEDIA_BLINK_LRU_H_
+#define MEDIA_BLINK_LRU_H_
+
+#include <list>
+#include <map>
+
+namespace media {
+
+template<typename T>
+class LRU {
+ public:
+ // Adds |x| to LRU.
+ // |x| must not already be in the LRU.
+ void Insert(const T& x) {
+ DCHECK(!Contains(x));
+ lru_.push_front(x);
+ pos_[x] = lru_.begin();
+ }
+
+ // Removes |x| from LRU.
+ // |x| must be in the LRU.
+ void Remove(const T& x) {
+ DCHECK(Contains(x)) << " x = " << x;
+ lru_.erase(pos_[x]);
+ pos_.erase(x);
+ }
+
+ // Moves |x| to front of LRU. (most recently used)
+ // If |x| is not in LRU, it is added.
+ void Use(const T& x) {
+ if (Contains(x)) Remove(x);
+ Insert(x);
+ }
+
+ bool Empty() const {
+ return lru_.empty();
+ }
+
+ // Returns the Least Recently Used T.
+ T Pop() {
+ DCHECK(!Empty());
+ T ret = lru_.back();
+ lru_.pop_back();
+ pos_.erase(ret);
+ return ret;
+ }
+
+ T Peek() const {
+ DCHECK(!Empty());
+ return lru_.back();
+ }
+
+ bool Contains(const T& x) const {
+ return pos_.find(x) != pos_.end();
+ }
+ private:
+ std::list<T> lru_;
+ std::map<T, typename std::list<T>::iterator> pos_;
+};
+
+} // namespace media
+
+#endif // MEDIA_BLINK_LRU_H

Powered by Google App Engine
This is Rietveld 408576698