[PATCH] lrucachedict: add copy method
Augie Fackler
raf at durin42.com
Thu Dec 31 10:20:05 CST 2015
On Wed, Dec 30, 2015 at 03:14:46PM -0600, Eric Sumner wrote:
> # HG changeset patch
> # User Eric Sumner <ericsumner at fb.com>
> # Date 1451509853 28800
> # Wed Dec 30 13:10:53 2015 -0800
> # Node ID eba7ebec991c95bcebafd0586dea9b6b82d535ca
> # Parent 23541bdd1610c08af247f9c8719045cf247ce541
> lrucachedict: add copy method
Queued, thanks.
>
> 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 --git a/mercurial/util.py b/mercurial/util.py
> --- a/mercurial/util.py
> +++ b/mercurial/util.py
> @@ -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 --git a/tests/test-lrucachedict.py b/tests/test-lrucachedict.py
> --- a/tests/test-lrucachedict.py
> +++ b/tests/test-lrucachedict.py
> @@ -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 --git a/tests/test-lrucachedict.py.out b/tests/test-lrucachedict.py.out
> --- a/tests/test-lrucachedict.py.out
> +++ b/tests/test-lrucachedict.py.out
> @@ -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
> _______________________________________________
> Mercurial-devel mailing list
> Mercurial-devel at selenic.com
> https://selenic.com/mailman/listinfo/mercurial-devel
More information about the Mercurial-devel
mailing list