D7680: util: implement sortdict.insert()

martinvonz (Martin von Zweigbergk) phabricator at mercurial-scm.org
Mon Dec 16 19:00:03 EST 2019


martinvonz created this revision.
Herald added a subscriber: mercurial-devel.
Herald added a reviewer: hg-reviewers.

REVISION SUMMARY
  As flagged by pytype (reported via Matt Harbison, thanks).

REPOSITORY
  rHG Mercurial

BRANCH
  default

REVISION DETAIL
  https://phab.mercurial-scm.org/D7680

AFFECTED FILES
  mercurial/util.py

CHANGE DETAILS

diff --git a/mercurial/util.py b/mercurial/util.py
--- a/mercurial/util.py
+++ b/mercurial/util.py
@@ -1253,6 +1253,9 @@
     >>> d2.update([(b'a', 2)])
     >>> list(d2.keys()) # should still be in last-set order
     ['b', 'a']
+    >>> d1.insert(1, b'a.5', 0.5)
+    >>> d1
+    sortdict([(b'a', 0), (b'a.5', 0.5), (b'b', 1)])
     '''
 
     def __setitem__(self, key, value):
@@ -1268,6 +1271,14 @@
             for k, v in src:
                 self[k] = v
 
+    def insert(self, position, key, value):
+        for (i, (k, v)) in list(enumerate(self.items())):
+            if i == position:
+                self[key] = value
+            if i >= position:
+                del self[k]
+                self[k] = v
+
 
 class cowdict(cow, dict):
     """copy-on-write dict



To: martinvonz, #hg-reviewers
Cc: mercurial-devel


More information about the Mercurial-devel mailing list