# HG changeset patch # User Bryan O'Sullivan # Date 1365631706 25200 # Node ID 856960173630ed4ba73fae6173ce63bd2107832a # Parent 38982de2b4eb0bf3818be50357a3968cfd126bba scmutil: add a dirs class This encapsulates the "multiset of directories" structures that are currently open-coded (and duplicated) in both the dirstate and context modules. This will be used, and optionally replaced by a C implementation, in upcoming changes. diff -r 38982de2b4eb -r 856960173630 mercurial/scmutil.py --- a/mercurial/scmutil.py Wed Apr 10 15:08:25 2013 -0700 +++ b/mercurial/scmutil.py Wed Apr 10 15:08:26 2013 -0700 @@ -891,6 +891,42 @@ except KeyError: raise AttributeError(self.name) +class dirs(object): + '''a multiset of directory names from a dirstate or manifest''' + + def __init__(self, map, skip=None): + self._dirs = {} + addpath = self.addpath + if util.safehasattr(map, 'iteritems') and skip is not None: + for f, s in map.iteritems(): + if s[0] != skip: + addpath(f) + else: + for f in map: + addpath(f) + + def addpath(self, path): + dirs = self._dirs + for base in finddirs(path): + if base in dirs: + dirs[base] += 1 + return + dirs[base] = 1 + + def delpath(self, path): + dirs = self._dirs + for base in finddirs(path): + if dirs[base] > 1: + dirs[base] -= 1 + return + del dirs[base] + + def __iter__(self): + return self._dirs.iterkeys() + + def __contains__(self, d): + return d in self._dirs + def finddirs(path): pos = path.rfind('/') while pos != -1: