##// END OF EJS Templates
smartset: convert set to list lazily...
Jun Wu -
r31015:1076f7eb default
parent child Browse files
Show More
@@ -171,8 +171,12 b' class baseset(abstractsmartset):'
171 self._set = data
171 self._set = data
172 # set has no order we pick one for stability purpose
172 # set has no order we pick one for stability purpose
173 self._ascending = True
173 self._ascending = True
174 data = list(data)
174 # converting set to list has a cost, do it lazily
175 self._list = data
175 data = None
176 else:
177 data = list(data)
178 if data is not None:
179 self._list = data
176 self._datarepr = datarepr
180 self._datarepr = datarepr
177
181
178 @util.propertycache
182 @util.propertycache
@@ -185,6 +189,12 b' class baseset(abstractsmartset):'
185 asclist.sort()
189 asclist.sort()
186 return asclist
190 return asclist
187
191
192 @util.propertycache
193 def _list(self):
194 # _list is only lazily constructed if we have _set
195 assert '_set' in self.__dict__
196 return list(self._set)
197
188 def __iter__(self):
198 def __iter__(self):
189 if self._ascending is None:
199 if self._ascending is None:
190 return iter(self._list)
200 return iter(self._list)
@@ -204,7 +214,7 b' class baseset(abstractsmartset):'
204 return self._set.__contains__
214 return self._set.__contains__
205
215
206 def __nonzero__(self):
216 def __nonzero__(self):
207 return bool(self._list)
217 return bool(len(self))
208
218
209 def sort(self, reverse=False):
219 def sort(self, reverse=False):
210 self._ascending = not bool(reverse)
220 self._ascending = not bool(reverse)
@@ -218,7 +228,10 b' class baseset(abstractsmartset):'
218 self._istopo = False
228 self._istopo = False
219
229
220 def __len__(self):
230 def __len__(self):
221 return len(self._list)
231 if '_list' in self.__dict__:
232 return len(self._list)
233 else:
234 return len(self._set)
222
235
223 def isascending(self):
236 def isascending(self):
224 """Returns True if the collection is ascending order, False if not.
237 """Returns True if the collection is ascending order, False if not.
General Comments 0
You need to be logged in to leave comments. Login now