comparison mercurial/revset.py @ 22825:0e8bb81b58b9

baseset: stop inheriting from built-in list class The baseset is doing more and more smartset magic and using its list-like property less and less. So we store the list of revisions in an explicit attribute and stop inheriting. This requires reimplementing some basic methods.
author Pierre-Yves David <pierre-yves.david@fb.com>
date Mon, 06 Oct 2014 11:03:30 -0700
parents e4eb4bfc3616
children 4ffb327e4719
comparison
equal deleted inserted replaced
22824:9271630f4720 22825:0e8bb81b58b9
2318 kwargs['ascending'] = True 2318 kwargs['ascending'] = True
2319 elif self.isdescending(): 2319 elif self.isdescending():
2320 kwargs['ascending'] = False 2320 kwargs['ascending'] = False
2321 return filteredset(self, condition, **kwargs) 2321 return filteredset(self, condition, **kwargs)
2322 2322
2323 class baseset(list, abstractsmartset): 2323 class baseset(abstractsmartset):
2324 """Basic data structure that represents a revset and contains the basic 2324 """Basic data structure that represents a revset and contains the basic
2325 operation that it should be able to perform. 2325 operation that it should be able to perform.
2326 2326
2327 Every method in this class should be implemented by any smartset class. 2327 Every method in this class should be implemented by any smartset class.
2328 """ 2328 """
2329 def __init__(self, data=()): 2329 def __init__(self, data=()):
2330 super(baseset, self).__init__(data) 2330 if not isinstance(data, list):
2331 data = list(data)
2332 self._list = data
2331 self._set = None 2333 self._set = None
2332 2334
2333 def set(self): 2335 def set(self):
2334 """Returns a set or a smartset containing all the elements. 2336 """Returns a set or a smartset containing all the elements.
2335 2337
2344 @util.propertycache 2346 @util.propertycache
2345 def __contains__(self): 2347 def __contains__(self):
2346 return self.set().__contains__ 2348 return self.set().__contains__
2347 2349
2348 def __nonzero__(self): 2350 def __nonzero__(self):
2349 return bool(len(self)) 2351 return bool(self._list)
2352
2353 def sort(self, reverse=False):
2354 self._list.sort(reverse=reverse)
2355
2356 def reverse(self):
2357 self._list.reverse()
2358
2359 def __iter__(self):
2360 return iter(self._list)
2361
2362 def __len__(self):
2363 return len(self._list)
2350 2364
2351 def __sub__(self, other): 2365 def __sub__(self, other):
2352 """Returns a new object with the substraction of the two collections. 2366 """Returns a new object with the substraction of the two collections.
2353 2367
2354 This is part of the mandatory API for smartset.""" 2368 This is part of the mandatory API for smartset."""
2387 This is part of the mandatory API for smartset.""" 2401 This is part of the mandatory API for smartset."""
2388 return False 2402 return False
2389 2403
2390 def first(self): 2404 def first(self):
2391 if self: 2405 if self:
2392 return self[0] 2406 return self._list[0]
2393 return None 2407 return None
2394 2408
2395 def last(self): 2409 def last(self):
2396 if self: 2410 if self:
2397 return self[-1] 2411 return self._list[-1]
2398 return None 2412 return None
2399 2413
2400 class filteredset(abstractsmartset): 2414 class filteredset(abstractsmartset):
2401 """Duck type for baseset class which iterates lazily over the revisions in 2415 """Duck type for baseset class which iterates lazily over the revisions in
2402 the subset and contains a function which tests for membership in the 2416 the subset and contains a function which tests for membership in the