comparison mercurial/util.py @ 39563:b31b01f93b11

util: properly copy lrucachedict instances Previously, copy() only worked if the cache was full. We teach copy() to only copy defined nodes. Differential Revision: https://phab.mercurial-scm.org/D4498
author Gregory Szorc <gregory.szorc@gmail.com>
date Thu, 06 Sep 2018 11:33:40 -0700
parents e00123f63410
children 5d75a3c16193
comparison
equal deleted inserted replaced
39562:067f7d2c7d60 39563:b31b01f93b11
1311 1311
1312 self._cache.clear() 1312 self._cache.clear()
1313 1313
1314 def copy(self): 1314 def copy(self):
1315 result = lrucachedict(self._capacity) 1315 result = lrucachedict(self._capacity)
1316
1317 # We copy entries by iterating in oldest-to-newest order so the copy
1318 # has the correct ordering.
1319
1320 # Find the first non-empty entry.
1316 n = self._head.prev 1321 n = self._head.prev
1317 # Iterate in oldest-to-newest order, so the copy has the right ordering 1322 while n.key is _notset and n is not self._head:
1323 n = n.prev
1324
1318 for i in range(len(self._cache)): 1325 for i in range(len(self._cache)):
1319 result[n.key] = n.value 1326 result[n.key] = n.value
1320 n = n.prev 1327 n = n.prev
1328
1321 return result 1329 return result
1322 1330
1323 def _movetohead(self, node): 1331 def _movetohead(self, node):
1324 """Mark a node as the newest, making it the new head. 1332 """Mark a node as the newest, making it the new head.
1325 1333