replace set-like dictionaries with real sets
authorMartin Geisler <mg@lazybytes.net>
Wed, 22 Apr 2009 00:57:28 +0200
changeset 8152 08e1baf924ca
parent 8151 127281884959
child 8153 616f20e1004a
replace set-like dictionaries with real sets Many of the dictionaries created by dict.fromkeys were emulating sets. These can now be replaced with real sets.
hgext/bugzilla.py
hgext/mq.py
hgext/record.py
mercurial/cmdutil.py
mercurial/commands.py
mercurial/copies.py
mercurial/hbisect.py
mercurial/localrepo.py
mercurial/match.py
mercurial/merge.py
mercurial/revlog.py
--- a/hgext/bugzilla.py	Wed Apr 22 00:56:06 2009 +0200
+++ b/hgext/bugzilla.py	Wed Apr 22 00:57:28 2009 +0200
@@ -177,12 +177,12 @@
         self.run('''select bug_id from longdescs where
                     bug_id in %s and thetext like "%%%s%%"''' %
                  (buglist(ids), short(node)))
-        unknown = dict.fromkeys(ids)
+        unknown = set(ids)
         for (id,) in self.cursor.fetchall():
             self.ui.status(_('bug %d already knows about changeset %s\n') %
                            (id, short(node)))
-            unknown.pop(id, None)
-        return util.sort(unknown.keys())
+            unknown.discard(id)
+        return util.sort(unknown)
 
     def notify(self, ids, committer):
         '''tell bugzilla to send mail.'''
--- a/hgext/mq.py	Wed Apr 22 00:56:06 2009 +0200
+++ b/hgext/mq.py	Wed Apr 22 00:57:28 2009 +0200
@@ -1338,7 +1338,7 @@
                 msg = ''
             return '%s%s' % (patchname, msg)
 
-        applied = dict.fromkeys([p.name for p in self.applied])
+        applied = set([p.name for p in self.applied])
         if length is None:
             length = len(self.series) - start
         if not missing:
@@ -1762,10 +1762,8 @@
         if sr.mq.applied:
             qbase = bin(sr.mq.applied[0].rev)
             if not hg.islocal(dest):
-                heads = dict.fromkeys(sr.heads())
-                for h in sr.heads(qbase):
-                    del heads[h]
-                destrev = heads.keys()
+                heads = set(sr.heads())
+                destrev = list(heads.difference(sr.heads(qbase)))
                 destrev.append(sr.changelog.parents(qbase)[0])
     elif sr.capable('lookup'):
         try:
--- a/hgext/record.py	Wed Apr 22 00:56:06 2009 +0200
+++ b/hgext/record.py	Wed Apr 22 00:57:28 2009 +0200
@@ -422,9 +422,9 @@
         chunks = filterpatch(ui, parsepatch(fp))
         del fp
 
-        contenders = {}
+        contenders = set()
         for h in chunks:
-            try: contenders.update(dict.fromkeys(h.files()))
+            try: contenders.update(set(h.files()))
             except AttributeError: pass
 
         changed = changes[0] + changes[1] + changes[2]
@@ -433,7 +433,7 @@
             ui.status(_('no changes to record\n'))
             return 0
 
-        modified = dict.fromkeys(changes[0])
+        modified = set(changes[0])
 
         # 2. backup changed files, so we can restore them in the end
         backups = {}
--- a/mercurial/cmdutil.py	Wed Apr 22 00:56:06 2009 +0200
+++ b/mercurial/cmdutil.py	Wed Apr 22 00:57:28 2009 +0200
@@ -1017,13 +1017,13 @@
     else:
         defrange = '-1:0'
     revs = revrange(repo, opts['rev'] or [defrange])
-    wanted = {}
+    wanted = set()
     slowpath = m.anypats() or (m.files() and opts.get('removed'))
     fncache = {}
 
     if not slowpath and not m.files():
         # No files, no patterns.  Display all revs.
-        wanted = dict.fromkeys(revs)
+        wanted = set(revs)
     copies = []
     if not slowpath:
         # Only files, no patterns.  Check the history of each file.
@@ -1071,7 +1071,7 @@
                         break
                     fncache.setdefault(rev, [])
                     fncache[rev].append(file_)
-                    wanted[rev] = 1
+                    wanted.add(rev)
                     if follow and copied:
                         copies.append(copied)
     if slowpath:
@@ -1089,7 +1089,7 @@
             matches = filter(m, changefiles)
             if matches:
                 fncache[rev] = matches
-                wanted[rev] = 1
+                wanted.add(rev)
 
     class followfilter:
         def __init__(self, onlyfirst=False):
@@ -1135,8 +1135,8 @@
         ff = followfilter()
         stop = min(revs[0], revs[-1])
         for x in xrange(rev, stop-1, -1):
-            if ff.match(x) and x in wanted:
-                del wanted[x]
+            if ff.match(x):
+                wanted.discard(x)
 
     def iterate():
         if follow and not m.files():
--- a/mercurial/commands.py	Wed Apr 22 00:56:06 2009 +0200
+++ b/mercurial/commands.py	Wed Apr 22 00:57:28 2009 +0200
@@ -1480,8 +1480,8 @@
         except AttributeError:
             ct = {}
 
-        modcmds = dict.fromkeys([c.split('|', 1)[0] for c in ct])
-        helplist(_('list of commands:\n\n'), modcmds.has_key)
+        modcmds = set([c.split('|', 1)[0] for c in ct])
+        helplist(_('list of commands:\n\n'), modcmds.__contains__)
 
     if name and name != 'shortlist':
         i = None
@@ -2503,14 +2503,14 @@
 
         m = cmdutil.matchfiles(repo, names)
         changes = repo.status(match=m)[:4]
-        modified, added, removed, deleted = map(dict.fromkeys, changes)
+        modified, added, removed, deleted = map(set, changes)
 
         # if f is a rename, also revert the source
         cwd = repo.getcwd()
         for f in added:
             src = repo.dirstate.copied(f)
             if src and src not in names and repo.dirstate[src] == 'r':
-                removed[src] = None
+                removed.add(src)
                 names[src] = (repo.pathto(src, cwd), True)
 
         def removeforget(abs):
@@ -2824,7 +2824,7 @@
 
     rev_ = "."
     names = (name1,) + names
-    if len(names) != len(dict.fromkeys(names)):
+    if len(names) != len(set(names)):
         raise util.Abort(_('tag names must be unique'))
     for n in names:
         if n in ['tip', '.', 'null']:
--- a/mercurial/copies.py	Wed Apr 22 00:56:06 2009 +0200
+++ b/mercurial/copies.py	Wed Apr 22 00:57:28 2009 +0200
@@ -160,12 +160,12 @@
     for f in u2:
         checkcopies(f, m2, m1)
 
-    diverge2 = {}
+    diverge2 = set()
     for of, fl in diverge.items():
         if len(fl) == 1:
             del diverge[of] # not actually divergent
         else:
-            diverge2.update(dict.fromkeys(fl)) # reverse map for below
+            diverge2.update(fl) # reverse map for below
 
     if fullcopy:
         repo.ui.debug(_("  all copies found (* = to merge, ! = divergent):\n"))
--- a/mercurial/hbisect.py	Wed Apr 22 00:56:06 2009 +0200
+++ b/mercurial/hbisect.py	Wed Apr 22 00:57:28 2009 +0200
@@ -24,7 +24,7 @@
     """
 
     clparents = changelog.parentrevs
-    skip = dict.fromkeys([changelog.rev(n) for n in state['skip']])
+    skip = set([changelog.rev(n) for n in state['skip']])
 
     def buildancestors(bad, good):
         # only the earliest bad revision matters
@@ -109,7 +109,7 @@
 
         for c in children.get(rev, []):
             if ancestors[c]:
-                ancestors[c] = dict.fromkeys(ancestors[c] + a).keys()
+                ancestors[c] = list(set(ancestors[c] + a))
             else:
                 ancestors[c] = a + [c]
 
--- a/mercurial/localrepo.py	Wed Apr 22 00:56:06 2009 +0200
+++ b/mercurial/localrepo.py	Wed Apr 22 00:57:28 2009 +0200
@@ -1335,7 +1335,7 @@
         if not unknown:
             return base.keys(), [], []
 
-        req = dict.fromkeys(unknown)
+        req = set(unknown)
         reqcnt = 0
 
         # search through remote branches
@@ -1375,7 +1375,7 @@
                     for p in n[2:4]:
                         if p not in req and p not in m:
                             r.append(p)
-                            req[p] = 1
+                            req.add(p)
                 seen[n[0]] = 1
 
             if r:
@@ -1447,15 +1447,15 @@
         self.ui.debug(_("common changesets up to ")
                       + " ".join(map(short, base.keys())) + "\n")
 
-        remain = dict.fromkeys(self.changelog.nodemap)
+        remain = set(self.changelog.nodemap)
 
         # prune everything remote has from the tree
-        del remain[nullid]
+        remain.remove(nullid)
         remove = base.keys()
         while remove:
             n = remove.pop(0)
             if n in remain:
-                del remain[n]
+                remain.remove(n)
                 for p in self.changelog.parents(n):
                     remove.append(p)
 
@@ -1670,11 +1670,11 @@
             has_cl_set, junk, junk = cl.nodesbetween(None, knownheads)
             junk = None
             # Transform the list into an ersatz set.
-            has_cl_set = dict.fromkeys(has_cl_set)
+            has_cl_set = set(has_cl_set)
         else:
             # If there were no known heads, the recipient cannot be assumed to
             # know about any changesets.
-            has_cl_set = {}
+            has_cl_set = set()
 
         # Make it easy to refer to self.manifest
         mnfst = self.manifest
@@ -1932,7 +1932,7 @@
 
         cl = self.changelog
         nodes = cl.findmissing(common)
-        revset = dict.fromkeys([cl.rev(n) for n in nodes])
+        revset = set([cl.rev(n) for n in nodes])
         self.changegroupinfo(nodes, source)
 
         def identity(x):
--- a/mercurial/match.py	Wed Apr 22 00:56:06 2009 +0200
+++ b/mercurial/match.py	Wed Apr 22 00:57:28 2009 +0200
@@ -5,7 +5,7 @@
         self._root = root
         self._cwd = cwd
         self._files = files
-        self._fmap = dict.fromkeys(files)
+        self._fmap = set(files)
         self.matchfn = mf
         self._anypats = ap
     def __call__(self, fn):
--- a/mercurial/merge.py	Wed Apr 22 00:56:06 2009 +0200
+++ b/mercurial/merge.py	Wed Apr 22 00:57:28 2009 +0200
@@ -166,7 +166,7 @@
         if repo.ui.configbool("merge", "followcopies", True):
             dirs = repo.ui.configbool("merge", "followdirs", True)
             copy, diverge = copies.copies(repo, p1, p2, pa, dirs)
-        copied = dict.fromkeys(copy.values())
+        copied = set(copy.values())
         for of, fl in diverge.iteritems():
             act("divergent renames", "dr", of, fl)
 
--- a/mercurial/revlog.py	Wed Apr 22 00:56:06 2009 +0200
+++ b/mercurial/revlog.py	Wed Apr 22 00:57:28 2009 +0200
@@ -613,10 +613,9 @@
         heads = [self.rev(n) for n in heads]
 
         # we want the ancestors, but inclusive
-        has = dict.fromkeys(self.ancestors(*common))
-        has[nullrev] = None
-        for r in common:
-            has[r] = None
+        has = set(self.ancestors(*common))
+        has.add(nullrev)
+        has.update(common)
 
         # take all ancestors from heads that aren't in has
         missing = {}
@@ -726,9 +725,8 @@
                 # any other roots.
                 lowestrev = nullrev
                 roots = [nullid]
-        # Transform our roots list into a 'set' (i.e. a dictionary where the
-        # values don't matter.
-        descendents = dict.fromkeys(roots, 1)
+        # Transform our roots list into a set.
+        descendents = set(roots)
         # Also, keep the original roots so we can filter out roots that aren't
         # 'real' roots (i.e. are descended from other roots).
         roots = descendents.copy()
@@ -752,14 +750,14 @@
                     p = tuple(self.parents(n))
                     # If any of its parents are descendents, it's not a root.
                     if (p[0] in descendents) or (p[1] in descendents):
-                        roots.pop(n)
+                        roots.remove(n)
             else:
                 p = tuple(self.parents(n))
                 # A node is a descendent if either of its parents are
                 # descendents.  (We seeded the dependents list with the roots
                 # up there, remember?)
                 if (p[0] in descendents) or (p[1] in descendents):
-                    descendents[n] = 1
+                    descendents.add(n)
                     isdescendent = True
             if isdescendent and ((ancestors is None) or (n in ancestors)):
                 # Only include nodes that are both descendents and ancestors.
@@ -778,7 +776,7 @@
                     for p in self.parents(n):
                         heads.pop(p, None)
         heads = [n for n in heads.iterkeys() if heads[n] != 0]
-        roots = roots.keys()
+        roots = list(roots)
         assert orderedout
         assert roots
         assert heads
@@ -807,7 +805,7 @@
             start = nullid
         if stop is None:
             stop = []
-        stoprevs = dict.fromkeys([self.rev(n) for n in stop])
+        stoprevs = set([self.rev(n) for n in stop])
         startrev = self.rev(start)
         reachable = {startrev: 1}
         heads = {startrev: 1}