comparison mercurial/util.py @ 40880:7cda0cacbbf6

util: implement pop() on lrucachedict This moves __delitem__() to pop() as the requirement is pretty much the same, and reimplement __delitem__() by using pop().
author Yuya Nishihara <yuya@tcha.org>
date Sun, 04 Nov 2018 16:57:05 +0900
parents 0c638ff69f5c
children 593f6359681d
comparison
equal deleted inserted replaced
40879:0c638ff69f5c 40880:7cda0cacbbf6
1318 1318
1319 def __setitem__(self, k, v): 1319 def __setitem__(self, k, v):
1320 self.insert(k, v) 1320 self.insert(k, v)
1321 1321
1322 def __delitem__(self, k): 1322 def __delitem__(self, k):
1323 node = self._cache.pop(k) 1323 self.pop(k)
1324
1325 def pop(self, k, default=_notset):
1326 try:
1327 node = self._cache.pop(k)
1328 except KeyError:
1329 if default is _notset:
1330 raise
1331 return default
1332 value = node.value
1324 self.totalcost -= node.cost 1333 self.totalcost -= node.cost
1325 node.markempty() 1334 node.markempty()
1326 1335
1327 # Temporarily mark as newest item before re-adjusting head to make 1336 # Temporarily mark as newest item before re-adjusting head to make
1328 # this node the oldest item. 1337 # this node the oldest item.
1329 self._movetohead(node) 1338 self._movetohead(node)
1330 self._head = node.next 1339 self._head = node.next
1340
1341 return value
1331 1342
1332 # Additional dict methods. 1343 # Additional dict methods.
1333 1344
1334 def get(self, k, default=None): 1345 def get(self, k, default=None):
1335 try: 1346 try: