summaryrefslogtreecommitdiff
path: root/pylru.py
diff options
context:
space:
mode:
Diffstat (limited to 'pylru.py')
-rw-r--r--pylru.py38
1 files changed, 29 insertions, 9 deletions
diff --git a/pylru.py b/pylru.py
index 55e4b67..27df250 100644
--- a/pylru.py
+++ b/pylru.py
@@ -178,23 +178,37 @@ class lrucache(object):
self.mtf(node)
self.head = node.next
+ def __iter__(self):
+
+ # Return an iterator that returns the keys in the cache in order from
+ # the most recently to least recently used. Does not modify the cache
+ # order.
+ for node in self.dli():
+ yield node.key
+
def items(self):
- # return the (key, value) pairs (from most recent to least)
- # without modifying the cache order
- return zip(self.keys(), self.values())
+ # Return an iterator that returns the (key, value) pairs in the cache
+ # in order from the most recently to least recently used. Does not
+ # modify the cache order.
+ for node in self.dli():
+ yield (node.key, node.obj)
def keys(self):
- # return the keys (from most recent to least) in the cache
- # does not modify the cache order
- return self.table.keys()
+ # Return an iterator that returns the keys in the cache in order from
+ # the most recently to least recently used. Does not modify the cache
+ # order.
+ for node in self.dli():
+ yield node.key
def values(self):
- # return the values in the cache (from most recent to least)
- # does not modify the cache order
- return [node.obj for node in self.table.values()]
+ # Return an iterator that returns the values in the cache in order from
+ # the most recently to least recently used. Does not modify the cache
+ # order.
+ for node in self.dli():
+ yield node.obj
def size(self, size=None):
@@ -260,6 +274,12 @@ class lrucache(object):
node.next.prev = node
node.prev.next = node
+ def dli(self):
+ node = self.head
+ for i in range(len(self)):
+ yield node
+ node = node.next
+
class lruwrap(object):