##// END OF EJS Templates
smartset: extract method to slice abstractsmartset...
Yuya Nishihara -
r32819:4710cc4d default
parent child Browse files
Show More
@@ -1169,18 +1169,7 def limit(repo, subset, x, order):
1169 if ofs < 0:
1169 if ofs < 0:
1170 raise error.ParseError(_("negative offset"))
1170 raise error.ParseError(_("negative offset"))
1171 os = getset(repo, fullreposet(repo), args['set'])
1171 os = getset(repo, fullreposet(repo), args['set'])
1172 result = []
1172 ls = os.slice(ofs, ofs + lim)
1173 it = iter(os)
1174 for x in xrange(ofs):
1175 y = next(it, None)
1176 if y is None:
1177 break
1178 for x in xrange(lim):
1179 y = next(it, None)
1180 if y is None:
1181 break
1182 result.append(y)
1183 ls = baseset(result, datarepr=('<limit n=%d, offset=%d, %r>', lim, ofs, os))
1184 if order == followorder and lim > 1:
1173 if order == followorder and lim > 1:
1185 return subset & ls
1174 return subset & ls
1186 return ls & subset
1175 return ls & subset
@@ -1199,14 +1188,7 def last(repo, subset, x, order):
1199 raise error.ParseError(_("negative number to select"))
1188 raise error.ParseError(_("negative number to select"))
1200 os = getset(repo, fullreposet(repo), l[0])
1189 os = getset(repo, fullreposet(repo), l[0])
1201 os.reverse()
1190 os.reverse()
1202 result = []
1191 ls = os.slice(0, lim)
1203 it = iter(os)
1204 for x in xrange(lim):
1205 y = next(it, None)
1206 if y is None:
1207 break
1208 result.append(y)
1209 ls = baseset(result, datarepr=('<last n=%d, %r>', lim, os))
1210 if order == followorder and lim > 1:
1192 if order == followorder and lim > 1:
1211 return subset & ls
1193 return subset & ls
1212 ls.reverse()
1194 ls.reverse()
@@ -8,6 +8,7
8 from __future__ import absolute_import
8 from __future__ import absolute_import
9
9
10 from . import (
10 from . import (
11 error,
11 util,
12 util,
12 )
13 )
13
14
@@ -155,6 +156,28 class abstractsmartset(object):
155 condition = util.cachefunc(condition)
156 condition = util.cachefunc(condition)
156 return filteredset(self, condition, condrepr)
157 return filteredset(self, condition, condrepr)
157
158
159 def slice(self, start, stop):
160 """Return new smartset that contains selected elements from this set"""
161 if start < 0 or stop < 0:
162 raise error.ProgrammingError('negative index not allowed')
163 return self._slice(start, stop)
164
165 def _slice(self, start, stop):
166 # sub classes may override this. start and stop must not be negative,
167 # but start > stop is allowed, which should be an empty set.
168 ys = []
169 it = iter(self)
170 for x in xrange(start):
171 y = next(it, None)
172 if y is None:
173 break
174 for x in xrange(stop - start):
175 y = next(it, None)
176 if y is None:
177 break
178 ys.append(y)
179 return baseset(ys, datarepr=('slice=%d:%d %r', start, stop, self))
180
158 class baseset(abstractsmartset):
181 class baseset(abstractsmartset):
159 """Basic data structure that represents a revset and contains the basic
182 """Basic data structure that represents a revset and contains the basic
160 operation that it should be able to perform.
183 operation that it should be able to perform.
@@ -1016,14 +1016,34 Test first (=limit) and last
1016 8
1016 8
1017 9
1017 9
1018
1018
1019 Test smartset.slice() by first/last()
1020
1021 (using unoptimized set, filteredset as example)
1022
1023 $ hg debugrevspec --no-show-revs -s '0:7 & branch("re:")'
1024 * set:
1025 <filteredset
1026 <spanset+ 0:8>,
1027 <branch 're:'>>
1028 $ log 'limit(0:7 & branch("re:"), 3, 4)'
1029 4
1030 5
1031 6
1032 $ log 'limit(7:0 & branch("re:"), 3, 4)'
1033 3
1034 2
1035 1
1036 $ log 'last(0:7 & branch("re:"), 2)'
1037 6
1038 7
1039
1019 Test order of first/last revisions
1040 Test order of first/last revisions
1020
1041
1021 $ hg debugrevspec -s 'first(4:0, 3) & 3:'
1042 $ hg debugrevspec -s 'first(4:0, 3) & 3:'
1022 * set:
1043 * set:
1023 <filteredset
1044 <filteredset
1024 <baseset
1045 <baseset slice=0:3
1025 <limit n=3, offset=0,
1046 <spanset- 0:5>>,
1026 <spanset- 0:5>>>,
1027 <spanset+ 3:10>>
1047 <spanset+ 3:10>>
1028 4
1048 4
1029 3
1049 3
@@ -1032,18 +1052,16 Test order of first/last revisions
1032 * set:
1052 * set:
1033 <filteredset
1053 <filteredset
1034 <spanset+ 3:10>,
1054 <spanset+ 3:10>,
1035 <baseset
1055 <baseset slice=0:3
1036 <limit n=3, offset=0,
1056 <spanset- 0:5>>>
1037 <spanset- 0:5>>>>
1038 3
1057 3
1039 4
1058 4
1040
1059
1041 $ hg debugrevspec -s 'last(4:0, 3) & :1'
1060 $ hg debugrevspec -s 'last(4:0, 3) & :1'
1042 * set:
1061 * set:
1043 <filteredset
1062 <filteredset
1044 <baseset
1063 <baseset slice=0:3
1045 <last n=3,
1064 <spanset+ 0:5>>,
1046 <spanset+ 0:5>>>,
1047 <spanset+ 0:2>>
1065 <spanset+ 0:2>>
1048 1
1066 1
1049 0
1067 0
@@ -1052,9 +1070,8 Test order of first/last revisions
1052 * set:
1070 * set:
1053 <filteredset
1071 <filteredset
1054 <spanset+ 0:2>,
1072 <spanset+ 0:2>,
1055 <baseset
1073 <baseset slice=0:3
1056 <last n=3,
1074 <spanset+ 0:5>>>
1057 <spanset+ 0:5>>>>
1058 0
1075 0
1059 1
1076 1
1060
1077
@@ -1950,9 +1967,8 ordering defined by it.
1950 define)
1967 define)
1951 * set:
1968 * set:
1952 <filteredset
1969 <filteredset
1953 <baseset
1970 <baseset slice=0:1
1954 <limit n=1, offset=0,
1971 <baseset [1, 0, 2]>>,
1955 <baseset [1, 0, 2]>>>,
1956 <spanset- 0:3>>
1972 <spanset- 0:3>>
1957 1
1973 1
1958
1974
@@ -1987,9 +2003,8 ordering defined by it.
1987 <filteredset
2003 <filteredset
1988 <spanset- 0:3>,
2004 <spanset- 0:3>,
1989 <not
2005 <not
1990 <baseset
2006 <baseset slice=0:1
1991 <last n=1,
2007 <baseset [1, 2, 0]>>>>
1992 <baseset [1, 2, 0]>>>>>
1993 2
2008 2
1994 0
2009 0
1995
2010
@@ -3613,9 +3628,8 issue2549 - correct optimizations
3613 ('symbol', '2')))
3628 ('symbol', '2')))
3614 * set:
3629 * set:
3615 <filteredset
3630 <filteredset
3616 <baseset
3631 <baseset slice=0:2
3617 <limit n=2, offset=0,
3632 <baseset [1, 2, 3]>>,
3618 <baseset [1, 2, 3]>>>,
3619 <not
3633 <not
3620 <baseset [2]>>>
3634 <baseset [2]>>>
3621 1
3635 1
@@ -3669,9 +3683,8 issue2549 - correct optimizations
3669 ('symbol', '2')))
3683 ('symbol', '2')))
3670 * set:
3684 * set:
3671 <filteredset
3685 <filteredset
3672 <baseset
3686 <baseset slice=0:1
3673 <last n=1,
3687 <baseset [2, 1]>>,
3674 <baseset [2, 1]>>>,
3675 <not
3688 <not
3676 <baseset [2]>>>
3689 <baseset [2]>>>
3677
3690
General Comments 0
You need to be logged in to leave comments. Login now