equal
deleted
inserted
replaced
169 if not isinstance(data, list): |
169 if not isinstance(data, list): |
170 if isinstance(data, set): |
170 if isinstance(data, set): |
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 |
179 def _set(self): |
183 def _set(self): |
180 return set(self._list) |
184 return set(self._list) |
183 def _asclist(self): |
187 def _asclist(self): |
184 asclist = self._list[:] |
188 asclist = self._list[:] |
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) |
191 elif self._ascending: |
201 elif self._ascending: |
192 return iter(self._asclist) |
202 return iter(self._asclist) |
202 @util.propertycache |
212 @util.propertycache |
203 def __contains__(self): |
213 def __contains__(self): |
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) |
211 self._istopo = False |
221 self._istopo = False |
212 |
222 |
216 else: |
226 else: |
217 self._ascending = not self._ascending |
227 self._ascending = not self._ascending |
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. |
225 |
238 |
226 This is part of the mandatory API for smartset.""" |
239 This is part of the mandatory API for smartset.""" |