mercurial/smartset.py
changeset 31020 2d1bf84046f6
parent 31019 74f77f1c2215
child 31066 c962bb6af909
equal deleted inserted replaced
31019:74f77f1c2215 31020:2d1bf84046f6
   169     >>> xs = baseset(set(x))
   169     >>> xs = baseset(set(x))
   170     >>> ys = baseset(set(y))
   170     >>> ys = baseset(set(y))
   171     >>> [list(i) for i in [xs + ys, xs & ys, xs - ys]]
   171     >>> [list(i) for i in [xs + ys, xs & ys, xs - ys]]
   172     [[0, 4, 6, 7, 3, 5], [6, 7], [0, 4]]
   172     [[0, 4, 6, 7, 3, 5], [6, 7], [0, 4]]
   173     >>> [type(i).__name__ for i in [xs + ys, xs & ys, xs - ys]]
   173     >>> [type(i).__name__ for i in [xs + ys, xs & ys, xs - ys]]
   174     ['addset', 'filteredset', 'filteredset']
   174     ['addset', 'baseset', 'baseset']
   175 
   175 
   176     Construct by a list-like:
   176     Construct by a list-like:
   177     >>> xs = baseset(x)
   177     >>> xs = baseset(x)
   178     >>> ys = baseset(i for i in y)
   178     >>> ys = baseset(i for i in y)
   179     >>> [list(i) for i in [xs + ys, xs & ys, xs - ys]]
   179     >>> [list(i) for i in [xs + ys, xs & ys, xs - ys]]
   189     >>> [list(i) for i in [xs + ys, xs & ys, xs - ys]]
   189     >>> [list(i) for i in [xs + ys, xs & ys, xs - ys]]
   190     [[4, 0, 7, 6, 5, 3], [7, 6], [4, 0]]
   190     [[4, 0, 7, 6, 5, 3], [7, 6], [4, 0]]
   191     >>> [type(i).__name__ for i in [xs + ys, xs & ys, xs - ys]]
   191     >>> [type(i).__name__ for i in [xs + ys, xs & ys, xs - ys]]
   192     ['addset', 'filteredset', 'filteredset']
   192     ['addset', 'filteredset', 'filteredset']
   193 
   193 
   194     With sort():
   194     With sort(), set optimization could be used:
   195     >>> xs.sort(reverse=True)
   195     >>> xs.sort(reverse=True)
   196     >>> [list(i) for i in [xs + ys, xs & ys, xs - ys]]
   196     >>> [list(i) for i in [xs + ys, xs & ys, xs - ys]]
   197     [[7, 6, 4, 0, 5, 3], [7, 6], [4, 0]]
   197     [[7, 6, 4, 0, 5, 3], [7, 6], [4, 0]]
   198     >>> [type(i).__name__ for i in [xs + ys, xs & ys, xs - ys]]
   198     >>> [type(i).__name__ for i in [xs + ys, xs & ys, xs - ys]]
   199     ['addset', 'filteredset', 'filteredset']
   199     ['addset', 'baseset', 'baseset']
   200 
   200 
   201     >>> ys.sort()
   201     >>> ys.sort()
   202     >>> [list(i) for i in [xs + ys, xs & ys, xs - ys]]
   202     >>> [list(i) for i in [xs + ys, xs & ys, xs - ys]]
   203     [[7, 6, 4, 0, 3, 5], [7, 6], [4, 0]]
   203     [[7, 6, 4, 0, 3, 5], [7, 6], [4, 0]]
   204     >>> [type(i).__name__ for i in [xs + ys, xs & ys, xs - ys]]
   204     >>> [type(i).__name__ for i in [xs + ys, xs & ys, xs - ys]]
   205     ['addset', 'filteredset', 'filteredset']
   205     ['addset', 'baseset', 'baseset']
   206     """
   206     """
   207     def __init__(self, data=(), datarepr=None, istopo=False):
   207     def __init__(self, data=(), datarepr=None, istopo=False):
   208         """
   208         """
   209         datarepr: a tuple of (format, obj, ...), a function or an object that
   209         datarepr: a tuple of (format, obj, ...), a function or an object that
   210                   provides a printable representation of the given data.
   210                   provides a printable representation of the given data.
   319             elif self._ascending:
   319             elif self._ascending:
   320                 return self._asclist[-1]
   320                 return self._asclist[-1]
   321             else:
   321             else:
   322                 return self._asclist[0]
   322                 return self._asclist[0]
   323         return None
   323         return None
       
   324 
       
   325     def _fastsetop(self, other, op):
       
   326         # try to use native set operations as fast paths
       
   327         if (type(other) is baseset and '_set' in other.__dict__ and '_set' in
       
   328             self.__dict__ and self._ascending is not None):
       
   329             s = baseset(data=getattr(self._set, op)(other._set))
       
   330             s._ascending = self._ascending
       
   331         else:
       
   332             s = getattr(super(baseset, self), op)(other)
       
   333         return s
       
   334 
       
   335     def __and__(self, other):
       
   336         return self._fastsetop(other, '__and__')
       
   337 
       
   338     def __sub__(self, other):
       
   339         return self._fastsetop(other, '__sub__')
   324 
   340 
   325     def __repr__(self):
   341     def __repr__(self):
   326         d = {None: '', False: '-', True: '+'}[self._ascending]
   342         d = {None: '', False: '-', True: '+'}[self._ascending]
   327         s = _formatsetrepr(self._datarepr)
   343         s = _formatsetrepr(self._datarepr)
   328         if not s:
   344         if not s: