Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b8be385

Browse files
committed
Simplify maxdict implementation.
... by relying on dicts maintaining insertion order (which is what was previously tracked separately in `_killkeys`). Note that both the original and new implementations actually evict keys by *insertion* order, not as LRU.
1 parent 91cb8a8 commit b8be385

1 file changed

Lines changed: 5 additions & 8 deletions

File tree

lib/matplotlib/cbook/__init__.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -584,18 +584,15 @@ class maxdict(dict):
584584
This doesn't override all the relevant methods to constrain the size,
585585
just ``__setitem__``, so use with caution.
586586
"""
587+
587588
def __init__(self, maxsize):
588-
dict.__init__(self)
589+
super().__init__()
589590
self.maxsize = maxsize
590-
self._killkeys = []
591591

592592
def __setitem__(self, k, v):
593-
if k not in self:
594-
if len(self) >= self.maxsize:
595-
del self[self._killkeys[0]]
596-
del self._killkeys[0]
597-
self._killkeys.append(k)
598-
dict.__setitem__(self, k, v)
593+
super().__setitem__(k, v)
594+
while len(self) >= self.maxsize:
595+
del self[next(iter(self))]
599596

600597

601598
class Stack:

0 commit comments

Comments
 (0)