Patchwork D7680: util: implement sortdict.insert()

login
register
mail settings
Submitter phabricator
Date Dec. 17, 2019, 12:03 a.m.
Message ID <712402d6491a752d047d63a624992317@localhost.localdomain>
Download mbox | patch
Permalink /patch/43894/
State Not Applicable
Headers show

Comments

phabricator - Dec. 17, 2019, 12:03 a.m.
martinvonz updated this revision to Diff 18783.

REPOSITORY
  rHG Mercurial

CHANGES SINCE LAST UPDATE
  https://phab.mercurial-scm.org/D7680?vs=18781&id=18783

BRANCH
  default

CHANGES SINCE LAST ACTION
  https://phab.mercurial-scm.org/D7680/new/

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

AFFECTED FILES
  mercurial/util.py

CHANGE DETAILS




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

Patch

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([('a', 0), ('a.5', 0.5), ('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