Rev 2253: Add a .copy() member. in http://bzr.arbash-meinel.com/branches/bzr/0.15-dev/list_patch
John Arbash Meinel
john at arbash-meinel.com
Tue Jan 30 17:07:48 GMT 2007
At http://bzr.arbash-meinel.com/branches/bzr/0.15-dev/list_patch
------------------------------------------------------------
revno: 2253
revision-id: john at arbash-meinel.com-20070130170743-8d4lsxgevxk4dspc
parent: john at arbash-meinel.com-20070130165322-t5q71sf9ei1co1w7
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: list_patch
timestamp: Tue 2007-01-30 11:07:43 -0600
message:
Add a .copy() member.
This allows us to copy the current state, without having to
rebuild any large lists.
modified:
bzrlib/hybrid_linked_list.py hybrid_linked_list.p-20070130001028-zlexwz74tzo56o2t-1
bzrlib/tests/test_hybrid_linked_list.py test_hybrid_linked_l-20070130001028-zlexwz74tzo56o2t-2
-------------- next part --------------
=== modified file 'bzrlib/hybrid_linked_list.py'
--- a/bzrlib/hybrid_linked_list.py 2007-01-30 16:53:22 +0000
+++ b/bzrlib/hybrid_linked_list.py 2007-01-30 17:07:43 +0000
@@ -273,3 +273,17 @@
self.delete(start, end)
if content:
self.insert(start, content)
+
+ def copy(self):
+ """Create a copy of self.
+
+ This just copies the internal dictionaries, etc. It doesn't create
+ copies of the contents. However, since contents are never modified,
+ that should be fine.
+ """
+ new_hll = HybridLinkedList()
+ new_hll._ranges_counter = self._ranges_counter
+ new_hll._head = self._head
+ new_hll._range_map = self._range_map.copy()
+ new_hll._links = self._links.copy()
+ return new_hll
=== modified file 'bzrlib/tests/test_hybrid_linked_list.py'
--- a/bzrlib/tests/test_hybrid_linked_list.py 2007-01-30 16:53:22 +0000
+++ b/bzrlib/tests/test_hybrid_linked_list.py 2007-01-30 17:07:43 +0000
@@ -97,6 +97,35 @@
3:(0, 1, [8]),
}, hll._range_map)
+ def test_copy(self):
+ hll = hybrid_linked_list.HybridLinkedList([0, 1, 2])
+ hll.insert(3, [3, 4])
+ hll.insert(5, [5])
+ self.assertEqual([0, 1, 2, 3, 4, 5], list(hll))
+
+ hll2 = hll.copy()
+ self.assertEqual([0, 1, 2, 3, 4, 5], list(hll2))
+
+ # After copying, the contents should be identical
+ self.assertEqual(hll._ranges_counter, hll2._ranges_counter)
+ self.assertEqual(hll._head, hll2._head)
+ self.assertEqual(hll._range_map, hll2._range_map)
+ self.assertEqual(hll._links, hll2._links)
+
+ # The content entries should be identical items, but the rest are not
+ for range_id, (start, end, content) in hll._range_map.iteritems():
+ start2, end2, content2 = hll2._range_map[range_id]
+ self.assertEqual(id(content), id(content2))
+
+ # Now modifying one should not effect the other
+ hll.delete(3, 5)
+ self.assertEqual([0, 1, 2, 5], list(hll))
+ self.assertEqual([0, 1, 2, 3, 4, 5], list(hll2))
+
+ hll2.insert(4, [6])
+ self.assertEqual([0, 1, 2, 5], list(hll))
+ self.assertEqual([0, 1, 2, 3, 6, 4, 5], list(hll2))
+
def test_delete(self):
hll = hybrid_linked_list.HybridLinkedList([0, 1, 2, 3, 4])
hll.delete(2, 3) # del list[2:3]
More information about the bazaar-commits
mailing list