# HG changeset patch # User Eric Sumner # Date 1451509853 28800 # Node ID 6cd3044985c2809c1eddd80827df2ba9ccfd91fd # Parent 44f717c879033f28d3f7e7dc9f34aa394d2fea3f lrucachedict: add copy method This diff implements the standard dict copy() method for lrucachedicts, which will be used in the pushrebase extension to make a copy of the manifestcache. diff -r 44f717c87903 -r 6cd3044985c2 mercurial/util.py --- a/mercurial/util.py Thu Dec 31 09:55:56 2015 +0100 +++ b/mercurial/util.py Wed Dec 30 13:10:53 2015 -0800 @@ -622,6 +622,15 @@ self._cache.clear() + def copy(self): + 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): """Mark a node as the newest, making it the new head. diff -r 44f717c87903 -r 6cd3044985c2 tests/test-lrucachedict.py --- a/tests/test-lrucachedict.py Thu Dec 31 09:55:56 2015 +0100 +++ b/tests/test-lrucachedict.py Wed Dec 30 13:10:53 2015 -0800 @@ -1,11 +1,11 @@ from mercurial import util -def printifpresent(d, xs): +def printifpresent(d, xs, name='d'): for x in xs: present = x in d - print "'%s' in d: %s" % (x, present) + print "'%s' in %s: %s" % (x, name, present) if present: - print "d['%s']: %s" % (x, d[x]) + print "%s['%s']: %s" % (name, x, d[x]) def test_lrucachedict(): d = util.lrucachedict(4) @@ -42,5 +42,29 @@ d['b'] printifpresent(d, ['a', 'b']) + # test copy method + d = util.lrucachedict(4) + d['a'] = 'va3' + d['b'] = 'vb3' + d['c'] = 'vc3' + d['d'] = 'vd3' + + dc = d.copy() + + # all of these should be present + print "\nAll of these should be present:" + printifpresent(dc, ['a', 'b', 'c', 'd'], 'dc') + + # 'a' should be dropped because it was least recently used + print "\nAll of these except 'a' should be present:" + dc['e'] = 've3' + printifpresent(dc, ['a', 'b', 'c', 'd', 'e'], 'dc') + + # contents and order of original dict should remain unchanged + print "\nThese should be in reverse alphabetical order and read 'v?3':" + dc['b'] = 'vb3_new' + for k in list(iter(d)): + print "d['%s']: %s" % (k, d[k]) + if __name__ == '__main__': test_lrucachedict() diff -r 44f717c87903 -r 6cd3044985c2 tests/test-lrucachedict.py.out --- a/tests/test-lrucachedict.py.out Thu Dec 31 09:55:56 2015 +0100 +++ b/tests/test-lrucachedict.py.out Wed Dec 30 13:10:53 2015 -0800 @@ -33,3 +33,30 @@ d['a']: 1 'b' in d: True d['b']: 2 + +All of these should be present: +'a' in dc: True +dc['a']: va3 +'b' in dc: True +dc['b']: vb3 +'c' in dc: True +dc['c']: vc3 +'d' in dc: True +dc['d']: vd3 + +All of these except 'a' should be present: +'a' in dc: False +'b' in dc: True +dc['b']: vb3 +'c' in dc: True +dc['c']: vc3 +'d' in dc: True +dc['d']: vd3 +'e' in dc: True +dc['e']: ve3 + +These should be in reverse alphabetical order and read 'v?3': +d['d']: vd3 +d['c']: vc3 +d['b']: vb3 +d['a']: va3