[PATCH 2 of 2] util: rename lrucachedict._capacity to capacity
Gregory Szorc
gregory.szorc at gmail.com
Wed Jan 6 05:18:13 UTC 2016
# HG changeset patch
# User Gregory Szorc <gregory.szorc at gmail.com>
# Date 1452057366 28800
# Tue Jan 05 21:16:06 2016 -0800
# Node ID 8ef96a23b22f43e3a1bde06f3f5c1468a62cc4c9
# Parent de3fa1bfeec15608289d07fc45382d0d62800440
util: rename lrucachedict._capacity to capacity
Make it a "public" attribute so others can use it.
diff --git a/mercurial/util.py b/mercurial/util.py
--- a/mercurial/util.py
+++ b/mercurial/util.py
@@ -535,25 +535,28 @@ class lrucachedict(object):
The dict consists of an actual backing dict - indexed by original
key - and a doubly linked circular list defining the order of entries in
the cache.
The head node is the newest entry in the cache. If the cache is full,
we recycle head.prev and make it the new head. Cache accesses result in
the node being moved to before the existing head and being marked as the
new head node.
+
+ Changing the ``.capacity`` of the cache after instantiation results in
+ undefined behavior.
"""
def __init__(self, max):
self._cache = {}
self._head = head = _lrucachenode()
head.prev = head
head.next = head
self._size = 1
- self._capacity = max
+ self.capacity = max
def __len__(self):
return len(self._cache)
def __contains__(self, k):
return k in self._cache
def __iter__(self):
@@ -571,17 +574,17 @@ class lrucachedict(object):
def __setitem__(self, k, v):
node = self._cache.get(k)
# Replace existing value and mark as newest.
if node is not None:
node.value = v
self._movetohead(node)
return
- if self._size < self._capacity:
+ if self._size < self.capacity:
node = self._addcapacity()
else:
# Grab the last/oldest item.
node = self._head.prev
# At capacity. Kill the old entry.
if node.key is not _notset:
del self._cache[node.key]
@@ -614,17 +617,17 @@ class lrucachedict(object):
n = self._head
while n.key is not _notset:
n.markempty()
n = n.next
self._cache.clear()
def copy(self):
- result = lrucachedict(self._capacity)
+ result = lrucachedict(self.capacity)
n = self._head.prev
# Iterate in oldest-to-newest order, so the copy has the right ordering
for i in range(len(self._cache)):
result[n.key] = n.value
n = n.prev
return result
def _movetohead(self, node):
More information about the Mercurial-devel
mailing list