Mercurial > public > mercurial-scm > hg
comparison mercurial/revset.py @ 22725:88e5483bfb20
lazyset: add order awareness to the class
Just a bit of extra code makes the lazyset aware of order. This renders
orderedlazyset useless.
At some point, the `subset` will become responsible for this ordering logic. But
we are not there yet because the various objects used as subsets are not good enough.
author | Pierre-Yves David <pierre-yves.david@fb.com> |
---|---|
date | Thu, 02 Oct 2014 19:14:03 -0500 |
parents | abdb46cf3b38 |
children | 169aa5e74b52 |
comparison
equal
deleted
inserted
replaced
22724:abdb46cf3b38 | 22725:88e5483bfb20 |
---|---|
2407 class lazyset(abstractsmartset): | 2407 class lazyset(abstractsmartset): |
2408 """Duck type for baseset class which iterates lazily over the revisions in | 2408 """Duck type for baseset class which iterates lazily over the revisions in |
2409 the subset and contains a function which tests for membership in the | 2409 the subset and contains a function which tests for membership in the |
2410 revset | 2410 revset |
2411 """ | 2411 """ |
2412 def __init__(self, subset, condition=lambda x: True): | 2412 def __init__(self, subset, condition=lambda x: True, ascending=None): |
2413 """ | 2413 """ |
2414 condition: a function that decide whether a revision in the subset | 2414 condition: a function that decide whether a revision in the subset |
2415 belongs to the revset or not. | 2415 belongs to the revset or not. |
2416 """ | 2416 """ |
2417 self._subset = subset | 2417 self._subset = subset |
2418 self._condition = condition | 2418 self._condition = condition |
2419 self._cache = {} | 2419 self._cache = {} |
2420 if ascending is not None: | |
2421 ascending = bool(ascending) | |
2422 self._ascending = ascending | |
2420 | 2423 |
2421 def __contains__(self, x): | 2424 def __contains__(self, x): |
2422 c = self._cache | 2425 c = self._cache |
2423 if x not in c: | 2426 if x not in c: |
2424 v = c[x] = x in self._subset and self._condition(x) | 2427 v = c[x] = x in self._subset and self._condition(x) |
2471 # Basic implementation to be changed in future patches. | 2474 # Basic implementation to be changed in future patches. |
2472 l = baseset([r for r in self]) | 2475 l = baseset([r for r in self]) |
2473 return l[x] | 2476 return l[x] |
2474 | 2477 |
2475 def sort(self, reverse=False): | 2478 def sort(self, reverse=False): |
2476 if not util.safehasattr(self._subset, 'sort'): | 2479 if self._ascending is None: |
2477 self._subset = baseset(self._subset) | 2480 if not util.safehasattr(self._subset, 'sort'): |
2478 self._subset.sort(reverse=reverse) | 2481 self._subset = baseset(self._subset) |
2482 self._subset.sort(reverse=reverse) | |
2483 self._ascending = not reverse | |
2484 elif bool(reverse) == self._ascending: | |
2485 self.reverse() | |
2479 | 2486 |
2480 def reverse(self): | 2487 def reverse(self): |
2481 self._subset.reverse() | 2488 self._subset.reverse() |
2489 if self._ascending is not None: | |
2490 self._ascending = not self._ascending | |
2482 | 2491 |
2483 def set(self): | 2492 def set(self): |
2484 return set([r for r in self]) | 2493 return set([r for r in self]) |
2485 | 2494 |
2486 def isascending(self): | 2495 def isascending(self): |
2487 return False | 2496 return self._ascending is not None and self._ascending |
2488 | 2497 |
2489 def isdescending(self): | 2498 def isdescending(self): |
2490 return False | 2499 return self._ascending is not None and not self._ascending |
2491 | 2500 |
2492 def filter(self, l): | 2501 def filter(self, l): |
2493 return lazyset(self, l) | 2502 return lazyset(self, l) |
2494 | 2503 |
2495 class orderedlazyset(_orderedsetmixin, lazyset): | 2504 class orderedlazyset(_orderedsetmixin, lazyset): |