From 501a1a37c0db7a48e82e6e18c319ab2dd2e779e8 Mon Sep 17 00:00:00 2001 From: Jay Hutchinson Date: Sun, 10 Oct 2010 13:19:19 -0500 Subject: Removed unnecessary __del__ from lrucache. Added a few comments. --- pylru.py | 26 ++++++-------------------- test.py | 4 ++++ 2 files changed, 10 insertions(+), 20 deletions(-) diff --git a/pylru.py b/pylru.py index 83e78ff..50dd597 100644 --- a/pylru.py +++ b/pylru.py @@ -51,10 +51,9 @@ class lrucache(object): self.table = {} - # Initalize the list with 'size' empty nodes. Create the first node and + # Initalize the list with one empty node. Create a node and # assign it to the 'head' variable, which represents the head node in the - # list. Then each iteration of the loop creates a subsequent node and - # inserts it directly after the head node. + # list. self.head = _dlnode() self.head.next = self.head self.head.prev = self.head @@ -189,7 +188,8 @@ class lrucache(object): return self.listSize - + # Increases the size of the cache by inserting n empty nodes at the tail of + # the list. def addTailNode(self, n): for i in range(n): node = _dlnode() @@ -201,7 +201,8 @@ class lrucache(object): self.listSize += n - + # Decreases the size of the list by removing n nodes from the tail of the + # list. def removeTailNode(self, n): assert self.listSize > 1 # Invarient. XXX REMOVE this line XXX for i in range(n): @@ -224,21 +225,6 @@ class lrucache(object): self.listSize -= n - def __del__(self): - # When we are finished with the cache, special care is taken to break the - # doubly linked list, so that there are no cycles. First all of the 'prev' - # links are broken. Then the 'next' link between the 'tail' node and the - # 'head' node is broken. - - tail = self.head.prev - - node = self.head - while node.prev is not None: - node = node.prev - node.next.prev = None - - tail.next = None - # This method adjusts the doubly linked list so that 'node' directly preeceds # the 'head' node. Note that it works even if 'node' already directly diff --git a/test.py b/test.py index fd849d0..8ed5fee 100644 --- a/test.py +++ b/test.py @@ -2,6 +2,9 @@ from pylru import * import random +# This tests PyLRU by fuzzing it with random operations, then checking the +# results against another, simpler, LRU cache implementation. + class simplelrucache: def __init__(self, size): @@ -161,6 +164,7 @@ def testDecorator(): if __name__ == '__main__': random.seed() + for i in range(20): testcache() -- cgit v1.2.1