comparison mercurial/smartset.py @ 32818:9ddb18ae342e

smartset: extract spanset factory to make it constructed without a repo This renames the spanset class to _spanset, and moves its __init__ to new spanset() function. spanset() is now a factory function. This allows us to construct a spanset without keeping a repo instance.
author Yuya Nishihara <yuya@tcha.org>
date Sun, 24 May 2015 11:07:14 +0900
parents e962c70c0aad
children 4710cc4dac99
comparison
equal deleted inserted replaced
32817:e962c70c0aad 32818:9ddb18ae342e
904 904
905 def __repr__(self): 905 def __repr__(self):
906 d = {False: '-', True: '+'}[self._ascending] 906 d = {False: '-', True: '+'}[self._ascending]
907 return '<%s%s>' % (type(self).__name__, d) 907 return '<%s%s>' % (type(self).__name__, d)
908 908
909 class spanset(abstractsmartset): 909 def spanset(repo, start=0, end=None):
910 """Create a spanset that represents a range of repository revisions
911
912 start: first revision included the set (default to 0)
913 end: first revision excluded (last+1) (default to len(repo))
914
915 Spanset will be descending if `end` < `start`.
916 """
917 if end is None:
918 end = len(repo)
919 ascending = start <= end
920 if not ascending:
921 start, end = end + 1, start + 1
922 return _spanset(start, end, ascending, repo.changelog.filteredrevs)
923
924 class _spanset(abstractsmartset):
910 """Duck type for baseset class which represents a range of revisions and 925 """Duck type for baseset class which represents a range of revisions and
911 can work lazily and without having all the range in memory 926 can work lazily and without having all the range in memory
912 927
913 Note that spanset(x, y) behave almost like xrange(x, y) except for two 928 Note that spanset(x, y) behave almost like xrange(x, y) except for two
914 notable points: 929 notable points:
915 - when x < y it will be automatically descending, 930 - when x < y it will be automatically descending,
916 - revision filtered with this repoview will be skipped. 931 - revision filtered with this repoview will be skipped.
917 932
918 """ 933 """
919 def __init__(self, repo, start=0, end=None): 934 def __init__(self, start, end, ascending, hiddenrevs):
920 """
921 start: first revision included the set
922 (default to 0)
923 end: first revision excluded (last+1)
924 (default to len(repo)
925
926 Spanset will be descending if `end` < `start`.
927 """
928 if end is None:
929 end = len(repo)
930 self._ascending = start <= end
931 if not self._ascending:
932 start, end = end + 1, start +1
933 self._start = start 935 self._start = start
934 self._end = end 936 self._end = end
935 self._hiddenrevs = repo.changelog.filteredrevs 937 self._ascending = ascending
938 self._hiddenrevs = hiddenrevs
936 939
937 def sort(self, reverse=False): 940 def sort(self, reverse=False):
938 self._ascending = not reverse 941 self._ascending = not reverse
939 942
940 def reverse(self): 943 def reverse(self):
1018 return x 1021 return x
1019 return None 1022 return None
1020 1023
1021 def __repr__(self): 1024 def __repr__(self):
1022 d = {False: '-', True: '+'}[self._ascending] 1025 d = {False: '-', True: '+'}[self._ascending]
1023 return '<%s%s %d:%d>' % (type(self).__name__, d, 1026 return '<%s%s %d:%d>' % (type(self).__name__.lstrip('_'), d,
1024 self._start, self._end) 1027 self._start, self._end)
1025 1028
1026 class fullreposet(spanset): 1029 class fullreposet(_spanset):
1027 """a set containing all revisions in the repo 1030 """a set containing all revisions in the repo
1028 1031
1029 This class exists to host special optimization and magic to handle virtual 1032 This class exists to host special optimization and magic to handle virtual
1030 revisions such as "null". 1033 revisions such as "null".
1031 """ 1034 """
1032 1035
1033 def __init__(self, repo): 1036 def __init__(self, repo):
1034 super(fullreposet, self).__init__(repo) 1037 super(fullreposet, self).__init__(0, len(repo), True,
1038 repo.changelog.filteredrevs)
1035 1039
1036 def __and__(self, other): 1040 def __and__(self, other):
1037 """As self contains the whole repo, all of the other set should also be 1041 """As self contains the whole repo, all of the other set should also be
1038 in self. Therefore `self & other = other`. 1042 in self. Therefore `self & other = other`.
1039 1043