Patchwork D7680: util: implement sortdict.insert()

login
register
mail settings
Submitter phabricator
Date Dec. 17, 2019, 8:52 a.m.
Message ID <f8137c9d2890d48b094b8d6cc26fd0a9@localhost.localdomain>
Download mbox | patch
Permalink /patch/43912/
State Not Applicable
Headers show

Comments

phabricator - Dec. 17, 2019, 8:52 a.m.
Closed by commit rHG68af0228fedc: util: implement sortdict.insert() (authored by martinvonz).
This revision was automatically updated to reflect the committed changes.

REPOSITORY
  rHG Mercurial

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

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, pulkit
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 enumerate(list(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