mercurial/revset.py
changeset 22862 9e5576f822cc
parent 22861 546fa6576815
child 22863 a1a02b516cca
equal deleted inserted replaced
22861:546fa6576815 22862:9e5576f822cc
  2297 
  2297 
  2298         `condition` is a callable which takes a revision number and returns a
  2298         `condition` is a callable which takes a revision number and returns a
  2299         boolean.
  2299         boolean.
  2300 
  2300 
  2301         This is part of the mandatory API for smartset."""
  2301         This is part of the mandatory API for smartset."""
  2302         kwargs = {}
  2302         return filteredset(self, condition)
  2303         if self.isascending():
       
  2304             kwargs['ascending'] = True
       
  2305         elif self.isdescending():
       
  2306             kwargs['ascending'] = False
       
  2307         return filteredset(self, condition, **kwargs)
       
  2308 
  2303 
  2309 class baseset(abstractsmartset):
  2304 class baseset(abstractsmartset):
  2310     """Basic data structure that represents a revset and contains the basic
  2305     """Basic data structure that represents a revset and contains the basic
  2311     operation that it should be able to perform.
  2306     operation that it should be able to perform.
  2312 
  2307 
  2404 class filteredset(abstractsmartset):
  2399 class filteredset(abstractsmartset):
  2405     """Duck type for baseset class which iterates lazily over the revisions in
  2400     """Duck type for baseset class which iterates lazily over the revisions in
  2406     the subset and contains a function which tests for membership in the
  2401     the subset and contains a function which tests for membership in the
  2407     revset
  2402     revset
  2408     """
  2403     """
  2409     def __init__(self, subset, condition=lambda x: True, ascending=None):
  2404     def __init__(self, subset, condition=lambda x: True):
  2410         """
  2405         """
  2411         condition: a function that decide whether a revision in the subset
  2406         condition: a function that decide whether a revision in the subset
  2412                    belongs to the revset or not.
  2407                    belongs to the revset or not.
  2413         """
  2408         """
  2414         self._subset = subset
  2409         self._subset = subset
  2415         self._condition = condition
  2410         self._condition = condition
  2416         self._cache = {}
  2411         self._cache = {}
  2417         if ascending is not None:
       
  2418             ascending = bool(ascending)
       
  2419         self._ascending = ascending
       
  2420 
  2412 
  2421     def __contains__(self, x):
  2413     def __contains__(self, x):
  2422         c = self._cache
  2414         c = self._cache
  2423         if x not in c:
  2415         if x not in c:
  2424             v = c[x] = x in self._subset and self._condition(x)
  2416             v = c[x] = x in self._subset and self._condition(x)
  2462         # Basic implementation to be changed in future patches.
  2454         # Basic implementation to be changed in future patches.
  2463         l = baseset([r for r in self])
  2455         l = baseset([r for r in self])
  2464         return l[x]
  2456         return l[x]
  2465 
  2457 
  2466     def sort(self, reverse=False):
  2458     def sort(self, reverse=False):
  2467         if self._ascending is None:
  2459         self._subset.sort(reverse=reverse)
  2468             if not util.safehasattr(self._subset, 'sort'):
       
  2469                 self._subset = baseset(self._subset)
       
  2470             self._subset.sort(reverse=reverse)
       
  2471             self._ascending = not reverse
       
  2472         elif bool(reverse) == self._ascending:
       
  2473             self.reverse()
       
  2474 
  2460 
  2475     def reverse(self):
  2461     def reverse(self):
  2476         self._subset.reverse()
  2462         self._subset.reverse()
  2477         if self._ascending is not None:
       
  2478             self._ascending = not self._ascending
       
  2479 
  2463 
  2480     def set(self):
  2464     def set(self):
  2481         return set([r for r in self])
  2465         return set([r for r in self])
  2482 
  2466 
  2483     def isascending(self):
  2467     def isascending(self):
  2484         return self._ascending is not None and self._ascending
  2468         return self._subset.isascending()
  2485 
  2469 
  2486     def isdescending(self):
  2470     def isdescending(self):
  2487         return self._ascending is not None and not self._ascending
  2471         return self._subset.isdescending()
  2488 
  2472 
  2489     def first(self):
  2473     def first(self):
  2490         for x in self:
  2474         for x in self:
  2491             return x
  2475             return x
  2492         return None
  2476         return None
  2493 
  2477 
  2494     def last(self):
  2478     def last(self):
  2495         it = None
  2479         it = None
  2496         if self._ascending is not None:
  2480         if self._subset.isascending:
  2497             if self._ascending:
  2481             it = self.fastdesc
  2498                 it = self.fastdesc
  2482         elif self._subset.isdescending:
  2499             else:
  2483             it = self.fastdesc
  2500                 it = self.fastasc
       
  2501         if it is None:
  2484         if it is None:
  2502             # slowly consume everything. This needs improvement
  2485             # slowly consume everything. This needs improvement
  2503             it = lambda: reversed(list(self))
  2486             it = lambda: reversed(list(self))
  2504         for x in it():
  2487         for x in it():
  2505             return x
  2488             return x