| 1 |
from pypy.interpreter import gateway |
| 2 |
from pypy.interpreter.baseobjspace import W_Root |
| 3 |
from pypy.interpreter.error import OperationError, oefmt |
| 4 |
from pypy.interpreter.signature import Signature |
| 5 |
from pypy.interpreter.typedef import TypeDef |
| 6 |
from pypy.objspace.std.bytesobject import W_BytesObject |
| 7 |
from pypy.objspace.std.listobject import is_plain_int1, plain_int_w |
| 8 |
from pypy.objspace.std.unicodeobject import W_UnicodeObject |
| 9 |
from pypy.objspace.std.util import IDTAG_SPECIAL, IDTAG_SHIFT, \ |
| 10 |
generic_alias_class_getitem, builtinclass_new_args_check |
| 11 |
|
| 12 |
from rpython.rlib.objectmodel import r_dict |
| 13 |
from rpython.rlib.objectmodel import iterkeys_with_hash, contains_with_hash |
| 14 |
from rpython.rlib.objectmodel import setitem_with_hash, delitem_with_hash |
| 15 |
from rpython.rlib.rarithmetic import intmask, r_uint |
| 16 |
from rpython.rlib import rerased, jit, rutf8 |
| 17 |
|
| 18 |
|
| 19 |
UNROLL_CUTOFF = 5 |
| 20 |
|
| 21 |
|
| 22 |
class W_BaseSetObject(W_Root): |
| 23 |
typedef = None |
| 24 |
|
| 25 |
def __init__(self, space, w_iterable=None): |
| 26 |
"""Initialize the set by taking ownership of 'setdata'.""" |
| 27 |
self.space = space |
| 28 |
set_strategy_and_setdata(space, self, w_iterable) |
| 29 |
|
| 30 |
def __repr__(self): |
| 31 |
"""representation for debugging purposes""" |
| 32 |
reprlist = [repr(w_item) for w_item in self.getkeys()] |
| 33 |
return "<%s(%s)(%s)>" % (self.__class__.__name__, self.strategy, ', '.join(reprlist)) |
| 34 |
|
| 35 |
def from_storage_and_strategy(self, storage, strategy): |
| 36 |
obj = self._newobj(self.space, None) |
| 37 |
assert isinstance(obj, W_BaseSetObject) |
| 38 |
obj.strategy = strategy |
| 39 |
obj.sstorage = storage |
| 40 |
return obj |
| 41 |
|
| 42 |
_lifeline_ = None |
| 43 |
def getweakref(self): |
| 44 |
return self._lifeline_ |
| 45 |
|
| 46 |
def setweakref(self, space, weakreflifeline): |
| 47 |
self._lifeline_ = weakreflifeline |
| 48 |
def delweakref(self): |
| 49 |
self._lifeline_ = None |
| 50 |
|
| 51 |
def switch_to_object_strategy(self, space): |
| 52 |
d = self.strategy.getdict_w(self) |
| 53 |
self.strategy = strategy = space.fromcache(ObjectSetStrategy) |
| 54 |
self.sstorage = strategy.erase(d) |
| 55 |
|
| 56 |
def switch_to_empty_strategy(self): |
| 57 |
self.strategy = strategy = self.space.fromcache(EmptySetStrategy) |
| 58 |
self.sstorage = strategy.get_empty_storage() |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
def clear(self): |
| 63 |
""" Removes all elements from the set. """ |
| 64 |
self.strategy.clear(self) |
| 65 |
|
| 66 |
def copy_real(self): |
| 67 |
""" Returns a clone of the set. Frozensets storages are also copied.""" |
| 68 |
return self.strategy.copy_real(self) |
| 69 |
|
| 70 |
def length(self): |
| 71 |
""" Returns the number of items inside the set. """ |
| 72 |
return self.strategy.length(self) |
| 73 |
|
| 74 |
def add(self, w_key): |
| 75 |
""" Adds an element to the set. The element must be wrapped. """ |
| 76 |
self.strategy.add(self, w_key) |
| 77 |
|
| 78 |
def remove(self, w_item): |
| 79 |
""" Removes the given element from the set. Element must be wrapped. """ |
| 80 |
return self.strategy.remove(self, w_item) |
| 81 |
|
| 82 |
def getdict_w(self): |
| 83 |
""" Returns a dict with all elements of the set. Needed only for switching to ObjectSetStrategy. """ |
| 84 |
return self.strategy.getdict_w(self) |
| 85 |
|
| 86 |
def listview_bytes(self): |
| 87 |
""" If this is a string set return its contents as a list of uwnrapped strings. Otherwise return None. """ |
| 88 |
return self.strategy.listview_bytes(self) |
| 89 |
|
| 90 |
def listview_ascii(self): |
| 91 |
""" If this is a unicode set return its contents as a list of uwnrapped unicodes. Otherwise return None. """ |
| 92 |
return self.strategy.listview_ascii(self) |
| 93 |
|
| 94 |
def listview_int(self): |
| 95 |
""" If this is an int set return its contents as a list of uwnrapped ints. Otherwise return None. """ |
| 96 |
return self.strategy.listview_int(self) |
| 97 |
|
| 98 |
def get_storage_copy(self): |
| 99 |
""" Returns a copy of the storage. Needed when we want to clone all elements from one set and |
| 100 |
put them into another. """ |
| 101 |
return self.strategy.get_storage_copy(self) |
| 102 |
|
| 103 |
def getkeys(self): |
| 104 |
""" Returns a list of all elements inside the set. Only used in __repr__. Use as less as possible.""" |
| 105 |
return self.strategy.getkeys(self) |
| 106 |
|
| 107 |
def difference(self, w_other): |
| 108 |
""" Returns a set with all items that are in this set, but not in w_other. W_other must be a set.""" |
| 109 |
return self.strategy.difference(self, w_other) |
| 110 |
|
| 111 |
def difference_update(self, w_other): |
| 112 |
""" As difference but overwrites the sets content with the result. W_other must be a set.""" |
| 113 |
self.strategy.difference_update(self, w_other) |
| 114 |
|
| 115 |
def symmetric_difference(self, w_other): |
| 116 |
""" Returns a set with all items that are either in this set or in w_other, but not in both. W_other must be a set. """ |
| 117 |
return self.strategy.symmetric_difference(self, w_other) |
| 118 |
|
| 119 |
def symmetric_difference_update(self, w_other): |
| 120 |
""" As symmetric_difference but overwrites the content of the set with the result. W_other must be a set.""" |
| 121 |
self.strategy.symmetric_difference_update(self, w_other) |
| 122 |
|
| 123 |
def intersect(self, w_other): |
| 124 |
""" Returns a set with all items that exists in both sets, this set and in w_other. W_other must be a set. """ |
| 125 |
return self.strategy.intersect(self, w_other) |
| 126 |
|
| 127 |
def intersect_update(self, w_other): |
| 128 |
""" Keeps only those elements found in both sets, removing all other elements. W_other must be a set.""" |
| 129 |
self.strategy.intersect_update(self, w_other) |
| 130 |
|
| 131 |
def issubset(self, w_other): |
| 132 |
""" Checks wether this set is a subset of w_other. W_other must be a set. """ |
| 133 |
return self.strategy.issubset(self, w_other) |
| 134 |
|
| 135 |
def isdisjoint(self, w_other): |
| 136 |
""" Checks wether this set and the w_other are completly different, i.e. have no equal elements. W_other must be a set.""" |
| 137 |
return self.strategy.isdisjoint(self, w_other) |
| 138 |
|
| 139 |
def update(self, w_other): |
| 140 |
""" Appends all elements from the given set to this set. W_other must be a set.""" |
| 141 |
self.strategy.update(self, w_other) |
| 142 |
|
| 143 |
def has_key(self, w_key): |
| 144 |
""" Checks wether this set contains the given wrapped key.""" |
| 145 |
return self.strategy.has_key(self, w_key) |
| 146 |
|
| 147 |
def equals(self, w_other): |
| 148 |
""" Checks wether this set and the given set are equal, i.e. contain the same elements. W_other must be a set.""" |
| 149 |
return self.strategy.equals(self, w_other) |
| 150 |
|
| 151 |
def iter(self): |
| 152 |
""" Returns an iterator of the elements from this set. """ |
| 153 |
return self.strategy.iter(self) |
| 154 |
|
| 155 |
def popitem(self): |
| 156 |
""" Removes an arbitrary element from the set. May raise KeyError if set is empty.""" |
| 157 |
return self.strategy.popitem(self) |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
def descr_init(self, space, w_iterable=None, __posonly__=None): |
| 162 |
_initialize_set(space, self, w_iterable) |
| 163 |
|
| 164 |
def descr_repr(self, space): |
| 165 |
return setrepr(space, space.get_objects_in_repr(), self) |
| 166 |
|
| 167 |
def descr_eq(self, space, w_other): |
| 168 |
if isinstance(w_other, W_BaseSetObject): |
| 169 |
return space.newbool(self.equals(w_other)) |
| 170 |
|
| 171 |
if not space.isinstance_w(w_other, space.w_set): |
| 172 |
return space.w_NotImplemented |
| 173 |
|
| 174 |
|
| 175 |
w_other_as_set = self._newobj(space, w_other) |
| 176 |
return space.newbool(self.equals(w_other_as_set)) |
| 177 |
|
| 178 |
def descr_ne(self, space, w_other): |
| 179 |
if isinstance(w_other, W_BaseSetObject): |
| 180 |
return space.newbool(not self.equals(w_other)) |
| 181 |
|
| 182 |
if not space.isinstance_w(w_other, space.w_set): |
| 183 |
return space.w_NotImplemented |
| 184 |
|
| 185 |
|
| 186 |
w_other_as_set = self._newobj(space, w_other) |
| 187 |
return space.newbool(not self.equals(w_other_as_set)) |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
def descr_lt(self, space, w_other): |
| 192 |
if not isinstance(w_other, W_BaseSetObject): |
| 193 |
return space.w_NotImplemented |
| 194 |
|
| 195 |
if self.length() >= w_other.length(): |
| 196 |
return space.w_False |
| 197 |
else: |
| 198 |
return self.descr_issubset(space, w_other) |
| 199 |
|
| 200 |
def descr_le(self, space, w_other): |
| 201 |
if not isinstance(w_other, W_BaseSetObject): |
| 202 |
return space.w_NotImplemented |
| 203 |
|
| 204 |
if self.length() > w_other.length(): |
| 205 |
return space.w_False |
| 206 |
return space.newbool(self.issubset(w_other)) |
| 207 |
|
| 208 |
def descr_gt(self, space, w_other): |
| 209 |
if not isinstance(w_other, W_BaseSetObject): |
| 210 |
return space.w_NotImplemented |
| 211 |
|
| 212 |
if self.length() <= w_other.length(): |
| 213 |
return space.w_False |
| 214 |
else: |
| 215 |
return self.descr_issuperset(space, w_other) |
| 216 |
|
| 217 |
def descr_ge(self, space, w_other): |
| 218 |
if not isinstance(w_other, W_BaseSetObject): |
| 219 |
return space.w_NotImplemented |
| 220 |
|
| 221 |
if self.length() < w_other.length(): |
| 222 |
return space.w_False |
| 223 |
return space.newbool(w_other.issubset(self)) |
| 224 |
|
| 225 |
def descr_len(self, space): |
| 226 |
return space.newint(self.length()) |
| 227 |
|
| 228 |
def descr_iter(self, space): |
| 229 |
return W_SetIterObject(space, self.iter()) |
| 230 |
|
| 231 |
def descr_contains(self, space, w_other): |
| 232 |
try: |
| 233 |
return space.newbool(self.has_key(w_other)) |
| 234 |
except OperationError as e: |
| 235 |
if e.match(space, space.w_TypeError): |
| 236 |
w_f = _convert_set_to_frozenset(space, w_other) |
| 237 |
if w_f is not None: |
| 238 |
return space.newbool(self.has_key(w_f)) |
| 239 |
raise |
| 240 |
|
| 241 |
def descr_sub(self, space, w_other): |
| 242 |
if not isinstance(w_other, W_BaseSetObject): |
| 243 |
return space.w_NotImplemented |
| 244 |
return self.difference(w_other) |
| 245 |
|
| 246 |
def descr_rsub(self, space, w_other): |
| 247 |
if not isinstance(w_other, W_BaseSetObject): |
| 248 |
return space.w_NotImplemented |
| 249 |
return w_other.difference(self) |
| 250 |
|
| 251 |
def descr_and(self, space, w_other): |
| 252 |
if not isinstance(w_other, W_BaseSetObject): |
| 253 |
return space.w_NotImplemented |
| 254 |
return self.intersect(w_other) |
| 255 |
descr_rand = descr_and |
| 256 |
|
| 257 |
def descr_or(self, space, w_other): |
| 258 |
if not isinstance(w_other, W_BaseSetObject): |
| 259 |
return space.w_NotImplemented |
| 260 |
w_copy = self.copy_real() |
| 261 |
w_copy.update(w_other) |
| 262 |
return w_copy |
| 263 |
descr_ror = descr_or |
| 264 |
|
| 265 |
def descr_xor(self, space, w_other): |
| 266 |
if not isinstance(w_other, W_BaseSetObject): |
| 267 |
return space.w_NotImplemented |
| 268 |
return self.symmetric_difference(w_other) |
| 269 |
descr_rxor = descr_xor |
| 270 |
|
| 271 |
def descr_copy(self, space): |
| 272 |
"""Return a shallow copy of a set.""" |
| 273 |
if type(self) is W_FrozensetObject: |
| 274 |
return self |
| 275 |
return self.copy_real() |
| 276 |
|
| 277 |
@gateway.unwrap_spec(others_w='args_w') |
| 278 |
def descr_difference(self, space, others_w): |
| 279 |
"""Return a new set with elements in the set that are not in the |
| 280 |
others.""" |
| 281 |
result = self.copy_real() |
| 282 |
result.descr_difference_update(space, others_w) |
| 283 |
return result |
| 284 |
|
| 285 |
@gateway.unwrap_spec(others_w='args_w') |
| 286 |
def descr_intersection(self, space, others_w): |
| 287 |
"""Return a new set with elements common to the set and all others.""" |
| 288 |
|
| 289 |
others_w = [self] + others_w |
| 290 |
|
| 291 |
|
| 292 |
startindex, startlength = 0, -1 |
| 293 |
for i in range(len(others_w)): |
| 294 |
w_other = others_w[i] |
| 295 |
try: |
| 296 |
length = space.int_w(space.len(w_other)) |
| 297 |
except OperationError as e: |
| 298 |
if (e.match(space, space.w_TypeError) or |
| 299 |
e.match(space, space.w_AttributeError)): |
| 300 |
continue |
| 301 |
raise |
| 302 |
|
| 303 |
if startlength == -1 or length < startlength: |
| 304 |
startindex = i |
| 305 |
startlength = length |
| 306 |
|
| 307 |
others_w[startindex], others_w[0] = others_w[0], others_w[startindex] |
| 308 |
|
| 309 |
result = self._newobj(space, others_w[0]) |
| 310 |
for i in range(1,len(others_w)): |
| 311 |
w_other = others_w[i] |
| 312 |
if isinstance(w_other, W_BaseSetObject): |
| 313 |
result.intersect_update(w_other) |
| 314 |
else: |
| 315 |
w_other_as_set = self._newobj(space, w_other) |
| 316 |
result.intersect_update(w_other_as_set) |
| 317 |
return result |
| 318 |
|
| 319 |
def descr_issubset(self, space, w_other): |
| 320 |
"""Report whether another set contains this set.""" |
| 321 |
if space.is_w(self, w_other): |
| 322 |
return space.w_True |
| 323 |
|
| 324 |
if isinstance(w_other, W_BaseSetObject): |
| 325 |
if self.length() > w_other.length(): |
| 326 |
return space.w_False |
| 327 |
return space.newbool(self.issubset(w_other)) |
| 328 |
|
| 329 |
w_other_as_set = self._newobj(space, w_other) |
| 330 |
if self.length() > w_other_as_set.length(): |
| 331 |
return space.w_False |
| 332 |
return space.newbool(self.issubset(w_other_as_set)) |
| 333 |
|
| 334 |
def descr_issuperset(self, space, w_other): |
| 335 |
"""Report whether this set contains another set.""" |
| 336 |
if space.is_w(self, w_other): |
| 337 |
return space.w_True |
| 338 |
|
| 339 |
if isinstance(w_other, W_BaseSetObject): |
| 340 |
if self.length() < w_other.length(): |
| 341 |
return space.w_False |
| 342 |
return space.newbool(w_other.issubset(self)) |
| 343 |
|
| 344 |
w_other_as_set = self._newobj(space, w_other) |
| 345 |
if self.length() < w_other_as_set.length(): |
| 346 |
return space.w_False |
| 347 |
return space.newbool(w_other_as_set.issubset(self)) |
| 348 |
|
| 349 |
def descr_symmetric_difference(self, space, w_other): |
| 350 |
"""Return the symmetric difference of two sets as a new set. |
| 351 |
|
| 352 |
(i.e. all elements that are in exactly one of the sets.)""" |
| 353 |
|
| 354 |
if isinstance(w_other, W_BaseSetObject): |
| 355 |
return self.symmetric_difference(w_other) |
| 356 |
|
| 357 |
w_other_as_set = self._newobj(space, w_other) |
| 358 |
return self.symmetric_difference(w_other_as_set) |
| 359 |
|
| 360 |
@gateway.unwrap_spec(others_w='args_w') |
| 361 |
def descr_union(self, space, others_w): |
| 362 |
"""Return a new set with elements from the set and all others.""" |
| 363 |
result = self.copy_real() |
| 364 |
for w_other in others_w: |
| 365 |
if isinstance(w_other, W_BaseSetObject): |
| 366 |
result.update(w_other) |
| 367 |
else: |
| 368 |
for w_key in space.listview(w_other): |
| 369 |
result.add(w_key) |
| 370 |
return result |
| 371 |
|
| 372 |
def descr_reduce(self, space): |
| 373 |
"""Return state information for pickling.""" |
| 374 |
return setreduce(space, self) |
| 375 |
|
| 376 |
def descr_isdisjoint(self, space, w_other): |
| 377 |
"""Return True if two sets have a null intersection.""" |
| 378 |
|
| 379 |
if isinstance(w_other, W_BaseSetObject): |
| 380 |
return space.newbool(self.isdisjoint(w_other)) |
| 381 |
|
| 382 |
|
| 383 |
for w_key in space.listview(w_other): |
| 384 |
if self.has_key(w_key): |
| 385 |
return space.w_False |
| 386 |
return space.w_True |
| 387 |
|
| 388 |
@gateway.unwrap_spec(others_w='args_w') |
| 389 |
def descr_difference_update(self, space, others_w): |
| 390 |
"""Update the set, removing elements found in others.""" |
| 391 |
for w_other in others_w: |
| 392 |
if isinstance(w_other, W_BaseSetObject): |
| 393 |
self.difference_update(w_other) |
| 394 |
else: |
| 395 |
w_other_as_set = self._newobj(space, w_other) |
| 396 |
self.difference_update(w_other_as_set) |
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
class W_SetObject(W_BaseSetObject): |
| 401 |
|
| 402 |
|
| 403 |
def __init__(self, space, w_iterable=None): |
| 404 |
"""Initialize the set by taking ownership of 'setdata'.""" |
| 405 |
W_BaseSetObject.__init__(self, space, w_iterable) |
| 406 |
|
| 407 |
def _newobj(self, space, w_iterable): |
| 408 |
"""Make a new set by taking ownership of 'w_iterable'.""" |
| 409 |
return W_SetObject(space, w_iterable) |
| 410 |
|
| 411 |
@staticmethod |
| 412 |
def descr_new(space, w_settype, __args__): |
| 413 |
w_obj = space.allocate_instance(W_SetObject, w_settype) |
| 414 |
W_SetObject.__init__(w_obj, space) |
| 415 |
return w_obj |
| 416 |
|
| 417 |
def descr_inplace_sub(self, space, w_other): |
| 418 |
if not isinstance(w_other, W_BaseSetObject): |
| 419 |
return space.w_NotImplemented |
| 420 |
self.difference_update(w_other) |
| 421 |
return self |
| 422 |
|
| 423 |
def descr_inplace_and(self, space, w_other): |
| 424 |
if not isinstance(w_other, W_BaseSetObject): |
| 425 |
return space.w_NotImplemented |
| 426 |
self.intersect_update(w_other) |
| 427 |
return self |
| 428 |
|
| 429 |
def descr_inplace_or(self, space, w_other): |
| 430 |
if not isinstance(w_other, W_BaseSetObject): |
| 431 |
return space.w_NotImplemented |
| 432 |
self.update(w_other) |
| 433 |
return self |
| 434 |
|
| 435 |
def descr_inplace_xor(self, space, w_other): |
| 436 |
if not isinstance(w_other, W_BaseSetObject): |
| 437 |
return space.w_NotImplemented |
| 438 |
self.descr_symmetric_difference_update(space, w_other) |
| 439 |
return self |
| 440 |
|
| 441 |
|
| 442 |
def descr_add(self, space, w_other): |
| 443 |
"""Add an element to a set. |
| 444 |
|
| 445 |
This has no effect if the element is already present.""" |
| 446 |
self.add(w_other) |
| 447 |
|
| 448 |
def descr_clear(self, space): |
| 449 |
"""Remove all elements from this set.""" |
| 450 |
self.clear() |
| 451 |
|
| 452 |
def _discard_from_set(self, space, w_item): |
| 453 |
""" |
| 454 |
Discard an element from a set, with automatic conversion to |
| 455 |
frozenset if the argument is a set. |
| 456 |
Returns True if successfully removed. |
| 457 |
""" |
| 458 |
try: |
| 459 |
deleted = self.remove(w_item) |
| 460 |
except OperationError as e: |
| 461 |
if not e.match(space, space.w_TypeError): |
| 462 |
raise |
| 463 |
else: |
| 464 |
w_f = _convert_set_to_frozenset(space, w_item) |
| 465 |
if w_f is None: |
| 466 |
raise |
| 467 |
deleted = self.remove(w_f) |
| 468 |
|
| 469 |
if self.length() == 0: |
| 470 |
self.switch_to_empty_strategy() |
| 471 |
return deleted |
| 472 |
|
| 473 |
def descr_discard(self, space, w_item): |
| 474 |
"""Remove an element from a set if it is a member. |
| 475 |
|
| 476 |
If the element is not a member, do nothing.""" |
| 477 |
self._discard_from_set(space, w_item) |
| 478 |
|
| 479 |
@gateway.unwrap_spec(others_w='args_w') |
| 480 |
def descr_intersection_update(self, space, others_w): |
| 481 |
"""Update the set, keeping only elements found in it and all others.""" |
| 482 |
result = self.descr_intersection(space, others_w) |
| 483 |
self.strategy = result.strategy |
| 484 |
self.sstorage = result.sstorage |
| 485 |
|
| 486 |
def descr_pop(self, space): |
| 487 |
"""Remove and return an arbitrary set element.""" |
| 488 |
return self.popitem() |
| 489 |
|
| 490 |
def descr_remove(self, space, w_item): |
| 491 |
"""Remove an element from a set; it must be a member. |
| 492 |
|
| 493 |
If the element is not a member, raise a KeyError.""" |
| 494 |
if not self._discard_from_set(space, w_item): |
| 495 |
space.raise_key_error(w_item) |
| 496 |
|
| 497 |
def descr_symmetric_difference_update(self, space, w_other): |
| 498 |
"""Update a set with the symmetric difference of itself and another.""" |
| 499 |
if isinstance(w_other, W_BaseSetObject): |
| 500 |
self.symmetric_difference_update(w_other) |
| 501 |
return |
| 502 |
w_other_as_set = self._newobj(space, w_other) |
| 503 |
self.symmetric_difference_update(w_other_as_set) |
| 504 |
|
| 505 |
@gateway.unwrap_spec(others_w='args_w') |
| 506 |
def descr_update(self, space, others_w): |
| 507 |
"""Update a set with the union of itself and another.""" |
| 508 |
self._descr_update(space, others_w) |
| 509 |
|
| 510 |
@jit.look_inside_iff(lambda self, space, others_w: |
| 511 |
jit.loop_unrolling_heuristic(others_w, len(others_w), UNROLL_CUTOFF)) |
| 512 |
def _descr_update(self, space, others_w): |
| 513 |
for w_other in others_w: |
| 514 |
if isinstance(w_other, W_BaseSetObject): |
| 515 |
self.update(w_other) |
| 516 |
else: |
| 517 |
_update_from_iterable(space, self, w_other) |
| 518 |
|
| 519 |
W_SetObject.typedef = TypeDef("set", |
| 520 |
__doc__ = """set(iterable) --> set object |
| 521 |
|
| 522 |
Build an unordered collection.""", |
| 523 |
__new__ = gateway.interp2app(W_SetObject.descr_new), |
| 524 |
__init__ = gateway.interp2app(W_SetObject.descr_init), |
| 525 |
__repr__ = gateway.interp2app(W_SetObject.descr_repr), |
| 526 |
__hash__ = None, |
| 527 |
|
| 528 |
__class_getitem__ = gateway.interp2app( |
| 529 |
generic_alias_class_getitem, as_classmethod=True), |
| 530 |
|
| 531 |
|
| 532 |
__eq__ = gateway.interp2app(W_SetObject.descr_eq), |
| 533 |
__ne__ = gateway.interp2app(W_SetObject.descr_ne), |
| 534 |
__lt__ = gateway.interp2app(W_SetObject.descr_lt), |
| 535 |
__le__ = gateway.interp2app(W_SetObject.descr_le), |
| 536 |
__gt__ = gateway.interp2app(W_SetObject.descr_gt), |
| 537 |
__ge__ = gateway.interp2app(W_SetObject.descr_ge), |
| 538 |
|
| 539 |
|
| 540 |
__len__ = gateway.interp2app(W_SetObject.descr_len), |
| 541 |
__iter__ = gateway.interp2app(W_SetObject.descr_iter), |
| 542 |
__contains__ = gateway.interp2app(W_SetObject.descr_contains), |
| 543 |
__sub__ = gateway.interp2app(W_SetObject.descr_sub), |
| 544 |
__rsub__ = gateway.interp2app(W_SetObject.descr_rsub), |
| 545 |
__and__ = gateway.interp2app(W_SetObject.descr_and), |
| 546 |
__rand__ = gateway.interp2app(W_SetObject.descr_rand), |
| 547 |
__or__ = gateway.interp2app(W_SetObject.descr_or), |
| 548 |
__ror__ = gateway.interp2app(W_SetObject.descr_ror), |
| 549 |
__xor__ = gateway.interp2app(W_SetObject.descr_xor), |
| 550 |
__rxor__ = gateway.interp2app(W_SetObject.descr_rxor), |
| 551 |
|
| 552 |
|
| 553 |
__isub__ = gateway.interp2app(W_SetObject.descr_inplace_sub), |
| 554 |
__iand__ = gateway.interp2app(W_SetObject.descr_inplace_and), |
| 555 |
__ior__ = gateway.interp2app(W_SetObject.descr_inplace_or), |
| 556 |
__ixor__ = gateway.interp2app(W_SetObject.descr_inplace_xor), |
| 557 |
|
| 558 |
|
| 559 |
__reduce__ = gateway.interp2app(W_SetObject.descr_reduce), |
| 560 |
copy = gateway.interp2app(W_SetObject.descr_copy), |
| 561 |
difference = gateway.interp2app(W_SetObject.descr_difference), |
| 562 |
intersection = gateway.interp2app(W_SetObject.descr_intersection), |
| 563 |
issubset = gateway.interp2app(W_SetObject.descr_issubset), |
| 564 |
issuperset = gateway.interp2app(W_SetObject.descr_issuperset), |
| 565 |
symmetric_difference = gateway.interp2app(W_SetObject.descr_symmetric_difference), |
| 566 |
union = gateway.interp2app(W_SetObject.descr_union), |
| 567 |
isdisjoint = gateway.interp2app(W_SetObject.descr_isdisjoint), |
| 568 |
|
| 569 |
|
| 570 |
add = gateway.interp2app(W_SetObject.descr_add), |
| 571 |
clear = gateway.interp2app(W_SetObject.descr_clear), |
| 572 |
difference_update = gateway.interp2app(W_SetObject.descr_difference_update), |
| 573 |
discard = gateway.interp2app(W_SetObject.descr_discard), |
| 574 |
intersection_update = gateway.interp2app(W_SetObject.descr_intersection_update), |
| 575 |
pop = gateway.interp2app(W_SetObject.descr_pop), |
| 576 |
remove = gateway.interp2app(W_SetObject.descr_remove), |
| 577 |
symmetric_difference_update = gateway.interp2app(W_SetObject.descr_symmetric_difference_update), |
| 578 |
update = gateway.interp2app(W_SetObject.descr_update) |
| 579 |
) |
| 580 |
set_typedef = W_SetObject.typedef |
| 581 |
|
| 582 |
|
| 583 |
class W_FrozensetObject(W_BaseSetObject): |
| 584 |
DEFAULT_HASH = -1 |
| 585 |
hash = DEFAULT_HASH |
| 586 |
|
| 587 |
def _cleanup_(self): |
| 588 |
|
| 589 |
|
| 590 |
self.hash = self.DEFAULT_HASH |
| 591 |
|
| 592 |
def is_w(self, space, w_other): |
| 593 |
if not isinstance(w_other, W_FrozensetObject): |
| 594 |
return False |
| 595 |
if self is w_other: |
| 596 |
return True |
| 597 |
if self.user_overridden_class or w_other.user_overridden_class: |
| 598 |
return False |
| 599 |
|
| 600 |
return 0 == w_other.length() == self.length() |
| 601 |
|
| 602 |
def immutable_unique_id(self, space): |
| 603 |
if self.user_overridden_class or self.length() > 0: |
| 604 |
return None |
| 605 |
|
| 606 |
uid = (259 << IDTAG_SHIFT) | IDTAG_SPECIAL |
| 607 |
return space.newint(uid) |
| 608 |
|
| 609 |
def _newobj(self, space, w_iterable): |
| 610 |
"""Make a new frozenset by taking ownership of 'w_iterable'.""" |
| 611 |
return W_FrozensetObject(space, w_iterable) |
| 612 |
|
| 613 |
@staticmethod |
| 614 |
def descr_new2(space, w_frozensettype, w_iterable=None, __posonly__=None, __args__=None): |
| 615 |
builtinclass_new_args_check(space, "frozenset", space.w_frozenset, w_frozensettype, __args__) |
| 616 |
if (space.is_w(w_frozensettype, space.w_frozenset) and |
| 617 |
w_iterable is not None and type(w_iterable) is W_FrozensetObject): |
| 618 |
return w_iterable |
| 619 |
w_obj = space.allocate_instance(W_FrozensetObject, w_frozensettype) |
| 620 |
W_FrozensetObject.__init__(w_obj, space, w_iterable) |
| 621 |
return w_obj |
| 622 |
|
| 623 |
def descr_hash(self, space): |
| 624 |
if self.hash != -1: |
| 625 |
return space.newint(self.hash) |
| 626 |
multi = r_uint(1822399083) + r_uint(1822399083) + 1 |
| 627 |
hash = r_uint(1927868237) |
| 628 |
hash *= r_uint(self.length() + 1) |
| 629 |
|
| 630 |
w_iterator = self.iter() |
| 631 |
while True: |
| 632 |
w_item = w_iterator.next_entry() |
| 633 |
if w_item is None: |
| 634 |
break |
| 635 |
h = space.hash_w(w_item) |
| 636 |
value = (r_uint(h ^ (h << 16) ^ 89869747) * multi) |
| 637 |
hash = hash ^ value |
| 638 |
hash ^= (hash >> 11) ^ (hash >> 25) |
| 639 |
hash = hash * 69069 + 907133923 |
| 640 |
hash = intmask(hash) |
| 641 |
if hash == -1: |
| 642 |
hash = 590923713 |
| 643 |
self.hash = hash |
| 644 |
|
| 645 |
return space.newint(hash) |
| 646 |
|
| 647 |
def cpyext_add_frozen(self, w_key): |
| 648 |
if self.hash != self.DEFAULT_HASH: |
| 649 |
return False |
| 650 |
self.add(w_key) |
| 651 |
return True |
| 652 |
|
| 653 |
W_FrozensetObject.typedef = TypeDef("frozenset", |
| 654 |
__doc__ = """frozenset(iterable) --> frozenset object |
| 655 |
|
| 656 |
Build an immutable unordered collection.""", |
| 657 |
__new__ = gateway.interp2app(W_FrozensetObject.descr_new2), |
| 658 |
__repr__ = gateway.interp2app(W_FrozensetObject.descr_repr), |
| 659 |
__hash__ = gateway.interp2app(W_FrozensetObject.descr_hash), |
| 660 |
|
| 661 |
__class_getitem__ = gateway.interp2app( |
| 662 |
generic_alias_class_getitem, as_classmethod=True), |
| 663 |
|
| 664 |
|
| 665 |
__eq__ = gateway.interp2app(W_FrozensetObject.descr_eq), |
| 666 |
__ne__ = gateway.interp2app(W_FrozensetObject.descr_ne), |
| 667 |
__lt__ = gateway.interp2app(W_FrozensetObject.descr_lt), |
| 668 |
__le__ = gateway.interp2app(W_FrozensetObject.descr_le), |
| 669 |
__gt__ = gateway.interp2app(W_FrozensetObject.descr_gt), |
| 670 |
__ge__ = gateway.interp2app(W_FrozensetObject.descr_ge), |
| 671 |
|
| 672 |
|
| 673 |
__len__ = gateway.interp2app(W_FrozensetObject.descr_len), |
| 674 |
__iter__ = gateway.interp2app(W_FrozensetObject.descr_iter), |
| 675 |
__contains__ = gateway.interp2app(W_FrozensetObject.descr_contains), |
| 676 |
__sub__ = gateway.interp2app(W_FrozensetObject.descr_sub), |
| 677 |
__rsub__ = gateway.interp2app(W_FrozensetObject.descr_rsub), |
| 678 |
__and__ = gateway.interp2app(W_FrozensetObject.descr_and), |
| 679 |
__rand__ = gateway.interp2app(W_FrozensetObject.descr_rand), |
| 680 |
__or__ = gateway.interp2app(W_FrozensetObject.descr_or), |
| 681 |
__ror__ = gateway.interp2app(W_FrozensetObject.descr_ror), |
| 682 |
__xor__ = gateway.interp2app(W_FrozensetObject.descr_xor), |
| 683 |
__rxor__ = gateway.interp2app(W_FrozensetObject.descr_rxor), |
| 684 |
|
| 685 |
|
| 686 |
__reduce__ = gateway.interp2app(W_FrozensetObject.descr_reduce), |
| 687 |
copy = gateway.interp2app(W_FrozensetObject.descr_copy), |
| 688 |
difference = gateway.interp2app(W_FrozensetObject.descr_difference), |
| 689 |
intersection = gateway.interp2app(W_FrozensetObject.descr_intersection), |
| 690 |
issubset = gateway.interp2app(W_FrozensetObject.descr_issubset), |
| 691 |
issuperset = gateway.interp2app(W_FrozensetObject.descr_issuperset), |
| 692 |
symmetric_difference = gateway.interp2app(W_FrozensetObject.descr_symmetric_difference), |
| 693 |
union = gateway.interp2app(W_FrozensetObject.descr_union), |
| 694 |
isdisjoint = gateway.interp2app(W_FrozensetObject.descr_isdisjoint) |
| 695 |
) |
| 696 |
frozenset_typedef = W_FrozensetObject.typedef |
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
class SetStrategy(object): |
| 701 |
def __init__(self, space): |
| 702 |
self.space = space |
| 703 |
|
| 704 |
def get_empty_dict(self): |
| 705 |
""" Returns an empty dictionary depending on the strategy. Used to initalize a new storage. """ |
| 706 |
raise NotImplementedError |
| 707 |
|
| 708 |
def get_empty_storage(self): |
| 709 |
""" Returns an empty storage (erased) object. Used to initialize an empty set.""" |
| 710 |
raise NotImplementedError |
| 711 |
|
| 712 |
def listview_bytes(self, w_set): |
| 713 |
return None |
| 714 |
|
| 715 |
def listview_ascii(self, w_set): |
| 716 |
return None |
| 717 |
|
| 718 |
def listview_int(self, w_set): |
| 719 |
return None |
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
def clear(self, w_set): |
| 730 |
raise NotImplementedError |
| 731 |
|
| 732 |
def copy_real(self, w_set): |
| 733 |
raise NotImplementedError |
| 734 |
|
| 735 |
def length(self, w_set): |
| 736 |
raise NotImplementedError |
| 737 |
|
| 738 |
def add(self, w_set, w_key): |
| 739 |
raise NotImplementedError |
| 740 |
|
| 741 |
def remove(self, w_set, w_item): |
| 742 |
raise NotImplementedError |
| 743 |
|
| 744 |
def getdict_w(self, w_set): |
| 745 |
raise NotImplementedError |
| 746 |
|
| 747 |
def get_storage_copy(self, w_set): |
| 748 |
raise NotImplementedError |
| 749 |
|
| 750 |
def getkeys(self, w_set): |
| 751 |
raise NotImplementedError |
| 752 |
|
| 753 |
def difference(self, w_set, w_other): |
| 754 |
raise NotImplementedError |
| 755 |
|
| 756 |
def difference_update(self, w_set, w_other): |
| 757 |
raise NotImplementedError |
| 758 |
|
| 759 |
def symmetric_difference(self, w_set, w_other): |
| 760 |
raise NotImplementedError |
| 761 |
|
| 762 |
def symmetric_difference_update(self, w_set, w_other): |
| 763 |
raise NotImplementedError |
| 764 |
|
| 765 |
def intersect(self, w_set, w_other): |
| 766 |
raise NotImplementedError |
| 767 |
|
| 768 |
def intersect_update(self, w_set, w_other): |
| 769 |
raise NotImplementedError |
| 770 |
|
| 771 |
def issubset(self, w_set, w_other): |
| 772 |
raise NotImplementedError |
| 773 |
|
| 774 |
def isdisjoint(self, w_set, w_other): |
| 775 |
raise NotImplementedError |
| 776 |
|
| 777 |
def update(self, w_set, w_other): |
| 778 |
raise NotImplementedError |
| 779 |
|
| 780 |
def has_key(self, w_set, w_key): |
| 781 |
raise NotImplementedError |
| 782 |
|
| 783 |
def equals(self, w_set, w_other): |
| 784 |
raise NotImplementedError |
| 785 |
|
| 786 |
def iter(self, w_set): |
| 787 |
raise NotImplementedError |
| 788 |
|
| 789 |
def popitem(self, w_set): |
| 790 |
raise NotImplementedError |
| 791 |
|
| 792 |
|
| 793 |
class EmptySetStrategy(SetStrategy): |
| 794 |
erase, unerase = rerased.new_erasing_pair("empty") |
| 795 |
erase = staticmethod(erase) |
| 796 |
unerase = staticmethod(unerase) |
| 797 |
|
| 798 |
def get_empty_storage(self): |
| 799 |
return self.erase(None) |
| 800 |
|
| 801 |
def is_correct_type(self, w_key): |
| 802 |
return False |
| 803 |
|
| 804 |
def length(self, w_set): |
| 805 |
return 0 |
| 806 |
|
| 807 |
def clear(self, w_set): |
| 808 |
pass |
| 809 |
|
| 810 |
def copy_real(self, w_set): |
| 811 |
storage = self.erase(None) |
| 812 |
clone = w_set.from_storage_and_strategy(storage, self) |
| 813 |
return clone |
| 814 |
|
| 815 |
def add(self, w_set, w_key): |
| 816 |
if is_plain_int1(w_key): |
| 817 |
strategy = self.space.fromcache(IntegerSetStrategy) |
| 818 |
elif type(w_key) is W_BytesObject: |
| 819 |
strategy = self.space.fromcache(BytesSetStrategy) |
| 820 |
elif type(w_key) is W_UnicodeObject and w_key.is_ascii(): |
| 821 |
strategy = self.space.fromcache(AsciiSetStrategy) |
| 822 |
elif self.space.type(w_key).compares_by_identity(): |
| 823 |
strategy = self.space.fromcache(IdentitySetStrategy) |
| 824 |
else: |
| 825 |
strategy = self.space.fromcache(ObjectSetStrategy) |
| 826 |
w_set.strategy = strategy |
| 827 |
w_set.sstorage = strategy.get_empty_storage() |
| 828 |
w_set.add(w_key) |
| 829 |
|
| 830 |
def remove(self, w_set, w_item): |
| 831 |
return False |
| 832 |
|
| 833 |
def getdict_w(self, w_set): |
| 834 |
return newset(self.space) |
| 835 |
|
| 836 |
def get_storage_copy(self, w_set): |
| 837 |
return w_set.sstorage |
| 838 |
|
| 839 |
def getkeys(self, w_set): |
| 840 |
return [] |
| 841 |
|
| 842 |
def has_key(self, w_set, w_key): |
| 843 |
|
| 844 |
self.space.hash_w(w_key) |
| 845 |
return False |
| 846 |
|
| 847 |
def equals(self, w_set, w_other): |
| 848 |
if w_other.strategy is self or w_other.length() == 0: |
| 849 |
return True |
| 850 |
return False |
| 851 |
|
| 852 |
def difference(self, w_set, w_other): |
| 853 |
return w_set.copy_real() |
| 854 |
|
| 855 |
def difference_update(self, w_set, w_other): |
| 856 |
pass |
| 857 |
|
| 858 |
def intersect(self, w_set, w_other): |
| 859 |
return w_set.copy_real() |
| 860 |
|
| 861 |
def intersect_update(self, w_set, w_other): |
| 862 |
pass |
| 863 |
|
| 864 |
def isdisjoint(self, w_set, w_other): |
| 865 |
return True |
| 866 |
|
| 867 |
def issubset(self, w_set, w_other): |
| 868 |
return True |
| 869 |
|
| 870 |
def symmetric_difference(self, w_set, w_other): |
| 871 |
return w_other.copy_real() |
| 872 |
|
| 873 |
def symmetric_difference_update(self, w_set, w_other): |
| 874 |
w_set.strategy = w_other.strategy |
| 875 |
w_set.sstorage = w_other.get_storage_copy() |
| 876 |
|
| 877 |
def update(self, w_set, w_other): |
| 878 |
w_set.strategy = w_other.strategy |
| 879 |
w_set.sstorage = w_other.get_storage_copy() |
| 880 |
|
| 881 |
def iter(self, w_set): |
| 882 |
return EmptyIteratorImplementation(self.space, self, w_set) |
| 883 |
|
| 884 |
def popitem(self, w_set): |
| 885 |
raise oefmt(self.space.w_KeyError, "pop from an empty set") |
| 886 |
|
| 887 |
|
| 888 |
class AbstractUnwrappedSetStrategy(object): |
| 889 |
_mixin_ = True |
| 890 |
|
| 891 |
def is_correct_type(self, w_key): |
| 892 |
""" Checks wether the given wrapped key fits this strategy.""" |
| 893 |
raise NotImplementedError |
| 894 |
|
| 895 |
def unwrap(self, w_item): |
| 896 |
""" Returns the unwrapped value of the given wrapped item.""" |
| 897 |
raise NotImplementedError |
| 898 |
|
| 899 |
def wrap(self, item): |
| 900 |
""" Returns a wrapped version of the given unwrapped item. """ |
| 901 |
raise NotImplementedError |
| 902 |
|
| 903 |
@jit.look_inside_iff(lambda self, list_w: |
| 904 |
jit.loop_unrolling_heuristic(list_w, len(list_w), UNROLL_CUTOFF)) |
| 905 |
def get_storage_from_list(self, list_w): |
| 906 |
setdata = self.get_empty_dict() |
| 907 |
for w_item in list_w: |
| 908 |
setdata[self.unwrap(w_item)] = None |
| 909 |
return self.erase(setdata) |
| 910 |
|
| 911 |
@jit.look_inside_iff(lambda self, items: |
| 912 |
jit.loop_unrolling_heuristic(items, len(items), UNROLL_CUTOFF)) |
| 913 |
def get_storage_from_unwrapped_list(self, items): |
| 914 |
setdata = self.get_empty_dict() |
| 915 |
for item in items: |
| 916 |
setdata[item] = None |
| 917 |
return self.erase(setdata) |
| 918 |
|
| 919 |
def length(self, w_set): |
| 920 |
return len(self.unerase(w_set.sstorage)) |
| 921 |
|
| 922 |
def clear(self, w_set): |
| 923 |
w_set.switch_to_empty_strategy() |
| 924 |
|
| 925 |
def copy_real(self, w_set): |
| 926 |
|
| 927 |
|
| 928 |
strategy = w_set.strategy |
| 929 |
d = self.unerase(w_set.sstorage) |
| 930 |
storage = self.erase(d.copy()) |
| 931 |
clone = w_set.from_storage_and_strategy(storage, strategy) |
| 932 |
return clone |
| 933 |
|
| 934 |
def add(self, w_set, w_key): |
| 935 |
if self.is_correct_type(w_key): |
| 936 |
d = self.unerase(w_set.sstorage) |
| 937 |
d[self.unwrap(w_key)] = None |
| 938 |
else: |
| 939 |
w_set.switch_to_object_strategy(self.space) |
| 940 |
w_set.add(w_key) |
| 941 |
|
| 942 |
def remove(self, w_set, w_item): |
| 943 |
d = self.unerase(w_set.sstorage) |
| 944 |
if not self.is_correct_type(w_item): |
| 945 |
|
| 946 |
w_set.switch_to_object_strategy(self.space) |
| 947 |
return w_set.remove(w_item) |
| 948 |
|
| 949 |
key = self.unwrap(w_item) |
| 950 |
try: |
| 951 |
del d[key] |
| 952 |
return True |
| 953 |
except KeyError: |
| 954 |
return False |
| 955 |
|
| 956 |
def getdict_w(self, w_set): |
| 957 |
result = newset(self.space) |
| 958 |
keys = self.unerase(w_set.sstorage).keys() |
| 959 |
for key in keys: |
| 960 |
result[self.wrap(key)] = None |
| 961 |
return result |
| 962 |
|
| 963 |
def get_storage_copy(self, w_set): |
| 964 |
d = self.unerase(w_set.sstorage) |
| 965 |
copy = self.erase(d.copy()) |
| 966 |
return copy |
| 967 |
|
| 968 |
def getkeys(self, w_set): |
| 969 |
keys = self.unerase(w_set.sstorage).keys() |
| 970 |
keys_w = [self.wrap(key) for key in keys] |
| 971 |
return keys_w |
| 972 |
|
| 973 |
def has_key(self, w_set, w_key): |
| 974 |
if not self.is_correct_type(w_key): |
| 975 |
|
| 976 |
w_set.switch_to_object_strategy(self.space) |
| 977 |
return w_set.has_key(w_key) |
| 978 |
d = self.unerase(w_set.sstorage) |
| 979 |
return self.unwrap(w_key) in d |
| 980 |
|
| 981 |
def equals(self, w_set, w_other): |
| 982 |
if w_set.length() != w_other.length(): |
| 983 |
return False |
| 984 |
if w_set.length() == 0: |
| 985 |
return True |
| 986 |
|
| 987 |
if w_set.strategy is w_other.strategy: |
| 988 |
return self._issubset_unwrapped(w_set, w_other) |
| 989 |
if not self.may_contain_equal_elements(w_other.strategy): |
| 990 |
return False |
| 991 |
items = self.unerase(w_set.sstorage).keys() |
| 992 |
for key in items: |
| 993 |
if not w_other.has_key(self.wrap(key)): |
| 994 |
return False |
| 995 |
return True |
| 996 |
|
| 997 |
def _difference_wrapped(self, w_set, w_other): |
| 998 |
iterator = self.unerase(w_set.sstorage).iterkeys() |
| 999 |
result_dict = self.get_empty_dict() |
| 1000 |
for key in iterator: |
| 1001 |
w_item = self.wrap(key) |
| 1002 |
if not w_other.has_key(w_item): |
| 1003 |
result_dict[key] = None |
| 1004 |
return self.erase(result_dict) |
| 1005 |
|
| 1006 |
def _difference_unwrapped(self, w_set, w_other): |
| 1007 |
self_dict = self.unerase(w_set.sstorage) |
| 1008 |
other_dict = self.unerase(w_other.sstorage) |
| 1009 |
result_dict = self.get_empty_dict() |
| 1010 |
for key, keyhash in iterkeys_with_hash(self_dict): |
| 1011 |
if not contains_with_hash(other_dict, key, keyhash): |
| 1012 |
setitem_with_hash(result_dict, key, keyhash, None) |
| 1013 |
return self.erase(result_dict) |
| 1014 |
|
| 1015 |
def _difference_base(self, w_set, w_other): |
| 1016 |
if self is w_other.strategy: |
| 1017 |
storage = self._difference_unwrapped(w_set, w_other) |
| 1018 |
elif not w_set.strategy.may_contain_equal_elements(w_other.strategy): |
| 1019 |
d = self.unerase(w_set.sstorage) |
| 1020 |
storage = self.erase(d.copy()) |
| 1021 |
else: |
| 1022 |
storage = self._difference_wrapped(w_set, w_other) |
| 1023 |
return storage |
| 1024 |
|
| 1025 |
def difference(self, w_set, w_other): |
| 1026 |
storage = self._difference_base(w_set, w_other) |
| 1027 |
w_newset = w_set.from_storage_and_strategy(storage, w_set.strategy) |
| 1028 |
return w_newset |
| 1029 |
|
| 1030 |
def _difference_update_unwrapped(self, w_set, w_other): |
| 1031 |
my_dict = self.unerase(w_set.sstorage) |
| 1032 |
if w_set.sstorage is w_other.sstorage: |
| 1033 |
my_dict.clear() |
| 1034 |
return |
| 1035 |
other_dict = self.unerase(w_other.sstorage) |
| 1036 |
for key, keyhash in iterkeys_with_hash(other_dict): |
| 1037 |
try: |
| 1038 |
delitem_with_hash(my_dict, key, keyhash) |
| 1039 |
except KeyError: |
| 1040 |
pass |
| 1041 |
|
| 1042 |
def _difference_update_wrapped(self, w_set, w_other): |
| 1043 |
w_iterator = w_other.iter() |
| 1044 |
while True: |
| 1045 |
w_item = w_iterator.next_entry() |
| 1046 |
if w_item is None: |
| 1047 |
break |
| 1048 |
w_set.remove(w_item) |
| 1049 |
|
| 1050 |
def difference_update(self, w_set, w_other): |
| 1051 |
if self.length(w_set) < w_other.strategy.length(w_other): |
| 1052 |
|
| 1053 |
storage = self._difference_base(w_set, w_other) |
| 1054 |
w_set.sstorage = storage |
| 1055 |
else: |
| 1056 |
|
| 1057 |
if self is w_other.strategy: |
| 1058 |
self._difference_update_unwrapped(w_set, w_other) |
| 1059 |
elif w_set.strategy.may_contain_equal_elements(w_other.strategy): |
| 1060 |
self._difference_update_wrapped(w_set, w_other) |
| 1061 |
|
| 1062 |
def _symmetric_difference_unwrapped(self, w_set, w_other): |
| 1063 |
d_new = self.get_empty_dict() |
| 1064 |
d_this = self.unerase(w_set.sstorage) |
| 1065 |
d_other = self.unerase(w_other.sstorage) |
| 1066 |
for key, keyhash in iterkeys_with_hash(d_other): |
| 1067 |
if not contains_with_hash(d_this, key, keyhash): |
| 1068 |
setitem_with_hash(d_new, key, keyhash, None) |
| 1069 |
for key, keyhash in iterkeys_with_hash(d_this): |
| 1070 |
if not contains_with_hash(d_other, key, keyhash): |
| 1071 |
setitem_with_hash(d_new, key, keyhash, None) |
| 1072 |
|
| 1073 |
storage = self.erase(d_new) |
| 1074 |
return storage |
| 1075 |
|
| 1076 |
def _symmetric_difference_wrapped(self, w_set, w_other): |
| 1077 |
newsetdata = newset(self.space) |
| 1078 |
for obj in self.unerase(w_set.sstorage): |
| 1079 |
w_item = self.wrap(obj) |
| 1080 |
if not w_other.has_key(w_item): |
| 1081 |
newsetdata[w_item] = None |
| 1082 |
|
| 1083 |
w_iterator = w_other.iter() |
| 1084 |
while True: |
| 1085 |
w_item = w_iterator.next_entry() |
| 1086 |
if w_item is None: |
| 1087 |
break |
| 1088 |
if not w_set.has_key(w_item): |
| 1089 |
newsetdata[w_item] = None |
| 1090 |
|
| 1091 |
strategy = self.space.fromcache(ObjectSetStrategy) |
| 1092 |
return strategy.erase(newsetdata) |
| 1093 |
|
| 1094 |
def _symmetric_difference_base(self, w_set, w_other): |
| 1095 |
if self is w_other.strategy: |
| 1096 |
strategy = w_set.strategy |
| 1097 |
storage = self._symmetric_difference_unwrapped(w_set, w_other) |
| 1098 |
else: |
| 1099 |
strategy = self.space.fromcache(ObjectSetStrategy) |
| 1100 |
storage = self._symmetric_difference_wrapped(w_set, w_other) |
| 1101 |
return storage, strategy |
| 1102 |
|
| 1103 |
def symmetric_difference(self, w_set, w_other): |
| 1104 |
if w_other.length() == 0: |
| 1105 |
return w_set.copy_real() |
| 1106 |
storage, strategy = self._symmetric_difference_base(w_set, w_other) |
| 1107 |
return w_set.from_storage_and_strategy(storage, strategy) |
| 1108 |
|
| 1109 |
def symmetric_difference_update(self, w_set, w_other): |
| 1110 |
if w_other.length() == 0: |
| 1111 |
return |
| 1112 |
storage, strategy = self._symmetric_difference_base(w_set, w_other) |
| 1113 |
w_set.strategy = strategy |
| 1114 |
w_set.sstorage = storage |
| 1115 |
|
| 1116 |
def _intersect_base(self, w_set, w_other): |
| 1117 |
if self is w_other.strategy: |
| 1118 |
strategy = self |
| 1119 |
if w_set.length() > w_other.length(): |
| 1120 |
|
| 1121 |
storage = self._intersect_unwrapped(w_other, w_set) |
| 1122 |
else: |
| 1123 |
storage = self._intersect_unwrapped(w_set, w_other) |
| 1124 |
elif not w_set.strategy.may_contain_equal_elements(w_other.strategy): |
| 1125 |
strategy = self.space.fromcache(EmptySetStrategy) |
| 1126 |
storage = strategy.get_empty_storage() |
| 1127 |
else: |
| 1128 |
strategy = self.space.fromcache(ObjectSetStrategy) |
| 1129 |
if w_set.length() > w_other.length(): |
| 1130 |
|
| 1131 |
storage = w_other.strategy._intersect_wrapped(w_other, w_set) |
| 1132 |
else: |
| 1133 |
storage = self._intersect_wrapped(w_set, w_other) |
| 1134 |
return storage, strategy |
| 1135 |
|
| 1136 |
def _intersect_wrapped(self, w_set, w_other): |
| 1137 |
result = newset(self.space) |
| 1138 |
for key in self.unerase(w_set.sstorage): |
| 1139 |
self.intersect_jmp.jit_merge_point() |
| 1140 |
w_key = self.wrap(key) |
| 1141 |
if w_other.has_key(w_key): |
| 1142 |
result[w_key] = None |
| 1143 |
|
| 1144 |
strategy = self.space.fromcache(ObjectSetStrategy) |
| 1145 |
return strategy.erase(result) |
| 1146 |
|
| 1147 |
def _intersect_unwrapped(self, w_set, w_other): |
| 1148 |
result = self.get_empty_dict() |
| 1149 |
d_this = self.unerase(w_set.sstorage) |
| 1150 |
d_other = self.unerase(w_other.sstorage) |
| 1151 |
for key, keyhash in iterkeys_with_hash(d_this): |
| 1152 |
if contains_with_hash(d_other, key, keyhash): |
| 1153 |
setitem_with_hash(result, key, keyhash, None) |
| 1154 |
return self.erase(result) |
| 1155 |
|
| 1156 |
def intersect(self, w_set, w_other): |
| 1157 |
storage, strategy = self._intersect_base(w_set, w_other) |
| 1158 |
return w_set.from_storage_and_strategy(storage, strategy) |
| 1159 |
|
| 1160 |
def intersect_update(self, w_set, w_other): |
| 1161 |
if w_set.length() > w_other.length(): |
| 1162 |
w_intersection = w_other.intersect(w_set) |
| 1163 |
strategy = w_intersection.strategy |
| 1164 |
storage = w_intersection.sstorage |
| 1165 |
else: |
| 1166 |
storage, strategy = self._intersect_base(w_set, w_other) |
| 1167 |
w_set.strategy = strategy |
| 1168 |
w_set.sstorage = storage |
| 1169 |
|
| 1170 |
def _issubset_unwrapped(self, w_set, w_other): |
| 1171 |
d_set = self.unerase(w_set.sstorage) |
| 1172 |
d_other = self.unerase(w_other.sstorage) |
| 1173 |
for key, keyhash in iterkeys_with_hash(d_set): |
| 1174 |
if not contains_with_hash(d_other, key, keyhash): |
| 1175 |
return False |
| 1176 |
return True |
| 1177 |
|
| 1178 |
def _issubset_wrapped(self, w_set, w_other): |
| 1179 |
for obj in self.unerase(w_set.sstorage): |
| 1180 |
w_item = self.wrap(obj) |
| 1181 |
if not w_other.has_key(w_item): |
| 1182 |
return False |
| 1183 |
return True |
| 1184 |
|
| 1185 |
def issubset(self, w_set, w_other): |
| 1186 |
if w_set.length() == 0: |
| 1187 |
return True |
| 1188 |
|
| 1189 |
if w_set.strategy is w_other.strategy: |
| 1190 |
return self._issubset_unwrapped(w_set, w_other) |
| 1191 |
elif not w_set.strategy.may_contain_equal_elements(w_other.strategy): |
| 1192 |
return False |
| 1193 |
else: |
| 1194 |
return self._issubset_wrapped(w_set, w_other) |
| 1195 |
|
| 1196 |
def _isdisjoint_unwrapped(self, w_set, w_other): |
| 1197 |
d_set = self.unerase(w_set.sstorage) |
| 1198 |
d_other = self.unerase(w_other.sstorage) |
| 1199 |
for key, keyhash in iterkeys_with_hash(d_set): |
| 1200 |
if contains_with_hash(d_other, key, keyhash): |
| 1201 |
return False |
| 1202 |
return True |
| 1203 |
|
| 1204 |
def _isdisjoint_wrapped(self, w_set, w_other): |
| 1205 |
d = self.unerase(w_set.sstorage) |
| 1206 |
for key in d: |
| 1207 |
if w_other.has_key(self.wrap(key)): |
| 1208 |
return False |
| 1209 |
return True |
| 1210 |
|
| 1211 |
def isdisjoint(self, w_set, w_other): |
| 1212 |
if w_other.length() == 0: |
| 1213 |
return True |
| 1214 |
if w_set.length() > w_other.length(): |
| 1215 |
return w_other.isdisjoint(w_set) |
| 1216 |
|
| 1217 |
if w_set.strategy is w_other.strategy: |
| 1218 |
return self._isdisjoint_unwrapped(w_set, w_other) |
| 1219 |
elif not w_set.strategy.may_contain_equal_elements(w_other.strategy): |
| 1220 |
return True |
| 1221 |
else: |
| 1222 |
return self._isdisjoint_wrapped(w_set, w_other) |
| 1223 |
|
| 1224 |
def update(self, w_set, w_other): |
| 1225 |
if self is w_other.strategy: |
| 1226 |
d_set = self.unerase(w_set.sstorage) |
| 1227 |
d_other = self.unerase(w_other.sstorage) |
| 1228 |
d_set.update(d_other) |
| 1229 |
return |
| 1230 |
if w_other.length() == 0: |
| 1231 |
return |
| 1232 |
w_set.switch_to_object_strategy(self.space) |
| 1233 |
w_set.update(w_other) |
| 1234 |
|
| 1235 |
def popitem(self, w_set): |
| 1236 |
storage = self.unerase(w_set.sstorage) |
| 1237 |
try: |
| 1238 |
|
| 1239 |
result = storage.popitem() |
| 1240 |
except KeyError: |
| 1241 |
|
| 1242 |
raise oefmt(self.space.w_KeyError, "pop from an empty set") |
| 1243 |
return self.wrap(result[0]) |
| 1244 |
|
| 1245 |
|
| 1246 |
class BytesSetStrategy(AbstractUnwrappedSetStrategy, SetStrategy): |
| 1247 |
erase, unerase = rerased.new_erasing_pair("bytes") |
| 1248 |
erase = staticmethod(erase) |
| 1249 |
unerase = staticmethod(unerase) |
| 1250 |
|
| 1251 |
intersect_jmp = jit.JitDriver(greens = [], reds = 'auto', |
| 1252 |
name='set(bytes).intersect') |
| 1253 |
|
| 1254 |
def get_empty_storage(self): |
| 1255 |
return self.erase({}) |
| 1256 |
|
| 1257 |
def get_empty_dict(self): |
| 1258 |
return {} |
| 1259 |
|
| 1260 |
def listview_bytes(self, w_set): |
| 1261 |
return self.unerase(w_set.sstorage).keys() |
| 1262 |
|
| 1263 |
def is_correct_type(self, w_key): |
| 1264 |
return type(w_key) is W_BytesObject |
| 1265 |
|
| 1266 |
def may_contain_equal_elements(self, strategy): |
| 1267 |
if strategy is self.space.fromcache(IntegerSetStrategy): |
| 1268 |
return False |
| 1269 |
elif strategy is self.space.fromcache(EmptySetStrategy): |
| 1270 |
return False |
| 1271 |
elif strategy is self.space.fromcache(IdentitySetStrategy): |
| 1272 |
return False |
| 1273 |
return True |
| 1274 |
|
| 1275 |
def unwrap(self, w_item): |
| 1276 |
return self.space.bytes_w(w_item) |
| 1277 |
|
| 1278 |
def wrap(self, item): |
| 1279 |
return self.space.newbytes(item) |
| 1280 |
|
| 1281 |
def iter(self, w_set): |
| 1282 |
return BytesIteratorImplementation(self.space, self, w_set) |
| 1283 |
|
| 1284 |
|
| 1285 |
class AsciiSetStrategy(AbstractUnwrappedSetStrategy, SetStrategy): |
| 1286 |
erase, unerase = rerased.new_erasing_pair("unicode") |
| 1287 |
erase = staticmethod(erase) |
| 1288 |
unerase = staticmethod(unerase) |
| 1289 |
|
| 1290 |
intersect_jmp = jit.JitDriver(greens = [], reds = 'auto', |
| 1291 |
name='set(unicode).intersect') |
| 1292 |
|
| 1293 |
def get_empty_storage(self): |
| 1294 |
return self.erase({}) |
| 1295 |
|
| 1296 |
def get_empty_dict(self): |
| 1297 |
return {} |
| 1298 |
|
| 1299 |
def listview_ascii(self, w_set): |
| 1300 |
return self.unerase(w_set.sstorage).keys() |
| 1301 |
|
| 1302 |
def is_correct_type(self, w_key): |
| 1303 |
return type(w_key) is W_UnicodeObject and w_key.is_ascii() |
| 1304 |
|
| 1305 |
def may_contain_equal_elements(self, strategy): |
| 1306 |
if strategy is self.space.fromcache(IntegerSetStrategy): |
| 1307 |
return False |
| 1308 |
elif strategy is self.space.fromcache(EmptySetStrategy): |
| 1309 |
return False |
| 1310 |
elif strategy is self.space.fromcache(IdentitySetStrategy): |
| 1311 |
return False |
| 1312 |
return True |
| 1313 |
|
| 1314 |
def unwrap(self, w_item): |
| 1315 |
return self.space.utf8_w(w_item) |
| 1316 |
|
| 1317 |
def wrap(self, item): |
| 1318 |
return self.space.newutf8(item, len(item)) |
| 1319 |
|
| 1320 |
def iter(self, w_set): |
| 1321 |
return UnicodeIteratorImplementation(self.space, self, w_set) |
| 1322 |
|
| 1323 |
|
| 1324 |
class IntegerSetStrategy(AbstractUnwrappedSetStrategy, SetStrategy): |
| 1325 |
erase, unerase = rerased.new_erasing_pair("integer") |
| 1326 |
erase = staticmethod(erase) |
| 1327 |
unerase = staticmethod(unerase) |
| 1328 |
|
| 1329 |
intersect_jmp = jit.JitDriver(greens = [], reds = 'auto', |
| 1330 |
name='set(int).intersect') |
| 1331 |
|
| 1332 |
def get_empty_storage(self): |
| 1333 |
return self.erase({}) |
| 1334 |
|
| 1335 |
def get_empty_dict(self): |
| 1336 |
return {} |
| 1337 |
|
| 1338 |
def listview_int(self, w_set): |
| 1339 |
return self.unerase(w_set.sstorage).keys() |
| 1340 |
|
| 1341 |
def is_correct_type(self, w_key): |
| 1342 |
return is_plain_int1(w_key) |
| 1343 |
|
| 1344 |
def may_contain_equal_elements(self, strategy): |
| 1345 |
if strategy is self.space.fromcache(BytesSetStrategy): |
| 1346 |
return False |
| 1347 |
elif strategy is self.space.fromcache(AsciiSetStrategy): |
| 1348 |
return False |
| 1349 |
elif strategy is self.space.fromcache(EmptySetStrategy): |
| 1350 |
return False |
| 1351 |
elif strategy is self.space.fromcache(IdentitySetStrategy): |
| 1352 |
return False |
| 1353 |
return True |
| 1354 |
|
| 1355 |
def unwrap(self, w_item): |
| 1356 |
return plain_int_w(self.space, w_item) |
| 1357 |
|
| 1358 |
def wrap(self, item): |
| 1359 |
return self.space.newint(item) |
| 1360 |
|
| 1361 |
def iter(self, w_set): |
| 1362 |
return IntegerIteratorImplementation(self.space, self, w_set) |
| 1363 |
|
| 1364 |
|
| 1365 |
class ObjectSetStrategy(AbstractUnwrappedSetStrategy, SetStrategy): |
| 1366 |
erase, unerase = rerased.new_erasing_pair("object") |
| 1367 |
erase = staticmethod(erase) |
| 1368 |
unerase = staticmethod(unerase) |
| 1369 |
|
| 1370 |
intersect_jmp = jit.JitDriver(greens = [], reds = 'auto', |
| 1371 |
name='set(object).intersect') |
| 1372 |
|
| 1373 |
def get_empty_storage(self): |
| 1374 |
return self.erase(self.get_empty_dict()) |
| 1375 |
|
| 1376 |
def get_empty_dict(self): |
| 1377 |
return newset(self.space) |
| 1378 |
|
| 1379 |
def is_correct_type(self, w_key): |
| 1380 |
return True |
| 1381 |
|
| 1382 |
def may_contain_equal_elements(self, strategy): |
| 1383 |
if strategy is self.space.fromcache(EmptySetStrategy): |
| 1384 |
return False |
| 1385 |
return True |
| 1386 |
|
| 1387 |
def unwrap(self, w_item): |
| 1388 |
return w_item |
| 1389 |
|
| 1390 |
def wrap(self, item): |
| 1391 |
return item |
| 1392 |
|
| 1393 |
def iter(self, w_set): |
| 1394 |
return RDictIteratorImplementation(self.space, self, w_set) |
| 1395 |
|
| 1396 |
def update(self, w_set, w_other): |
| 1397 |
d_obj = self.unerase(w_set.sstorage) |
| 1398 |
|
| 1399 |
|
| 1400 |
if w_other.strategy is self: |
| 1401 |
d_other = self.unerase(w_other.sstorage) |
| 1402 |
d_obj.update(d_other) |
| 1403 |
return |
| 1404 |
|
| 1405 |
w_iterator = w_other.iter() |
| 1406 |
while True: |
| 1407 |
w_item = w_iterator.next_entry() |
| 1408 |
if w_item is None: |
| 1409 |
break |
| 1410 |
d_obj[w_item] = None |
| 1411 |
|
| 1412 |
class IdentitySetStrategy(AbstractUnwrappedSetStrategy, SetStrategy): |
| 1413 |
erase, unerase = rerased.new_erasing_pair("identityset") |
| 1414 |
erase = staticmethod(erase) |
| 1415 |
unerase = staticmethod(unerase) |
| 1416 |
|
| 1417 |
intersect_jmp = jit.JitDriver(greens = [], reds = 'auto', |
| 1418 |
name='set(identity).intersect') |
| 1419 |
|
| 1420 |
def get_empty_storage(self): |
| 1421 |
return self.erase({}) |
| 1422 |
|
| 1423 |
def get_empty_dict(self): |
| 1424 |
return {} |
| 1425 |
|
| 1426 |
def is_correct_type(self, w_key): |
| 1427 |
w_type = self.space.type(w_key) |
| 1428 |
return w_type.compares_by_identity() |
| 1429 |
|
| 1430 |
def may_contain_equal_elements(self, strategy): |
| 1431 |
|
| 1432 |
if strategy is self.space.fromcache(EmptySetStrategy): |
| 1433 |
return False |
| 1434 |
if strategy is self.space.fromcache(IntegerSetStrategy): |
| 1435 |
return False |
| 1436 |
if strategy is self.space.fromcache(BytesSetStrategy): |
| 1437 |
return False |
| 1438 |
if strategy is self.space.fromcache(AsciiSetStrategy): |
| 1439 |
return False |
| 1440 |
return True |
| 1441 |
|
| 1442 |
def unwrap(self, w_item): |
| 1443 |
return w_item |
| 1444 |
|
| 1445 |
def wrap(self, item): |
| 1446 |
return item |
| 1447 |
|
| 1448 |
def iter(self, w_set): |
| 1449 |
return IdentityIteratorImplementation(self.space, self, w_set) |
| 1450 |
|
| 1451 |
class IteratorImplementation(object): |
| 1452 |
def __init__(self, space, strategy, implementation): |
| 1453 |
self.space = space |
| 1454 |
self.strategy = strategy |
| 1455 |
self.setimplementation = implementation |
| 1456 |
self.len = implementation.length() |
| 1457 |
self.pos = 0 |
| 1458 |
|
| 1459 |
def next(self): |
| 1460 |
if self.setimplementation is None: |
| 1461 |
return None |
| 1462 |
if self.len != self.setimplementation.length(): |
| 1463 |
self.len = -1 |
| 1464 |
raise oefmt(self.space.w_RuntimeError, |
| 1465 |
"set changed size during iteration") |
| 1466 |
|
| 1467 |
if self.pos < self.len: |
| 1468 |
result = self.next_entry() |
| 1469 |
self.pos += 1 |
| 1470 |
if self.strategy is self.setimplementation.strategy: |
| 1471 |
return result |
| 1472 |
else: |
| 1473 |
|
| 1474 |
|
| 1475 |
|
| 1476 |
if not self.setimplementation.has_key(result): |
| 1477 |
self.len = -1 |
| 1478 |
raise oefmt(self.space.w_RuntimeError, |
| 1479 |
"dictionary changed during iteration") |
| 1480 |
return result |
| 1481 |
|
| 1482 |
self.setimplementation = None |
| 1483 |
return None |
| 1484 |
|
| 1485 |
def next_entry(self): |
| 1486 |
""" Purely abstract method |
| 1487 |
""" |
| 1488 |
raise NotImplementedError |
| 1489 |
|
| 1490 |
def length(self): |
| 1491 |
if self.setimplementation is not None and self.len != -1: |
| 1492 |
return self.len - self.pos |
| 1493 |
return 0 |
| 1494 |
|
| 1495 |
|
| 1496 |
class EmptyIteratorImplementation(IteratorImplementation): |
| 1497 |
def next_entry(self): |
| 1498 |
return None |
| 1499 |
|
| 1500 |
|
| 1501 |
class BytesIteratorImplementation(IteratorImplementation): |
| 1502 |
def __init__(self, space, strategy, w_set): |
| 1503 |
IteratorImplementation.__init__(self, space, strategy, w_set) |
| 1504 |
d = strategy.unerase(w_set.sstorage) |
| 1505 |
self.iterator = d.iterkeys() |
| 1506 |
|
| 1507 |
def next_entry(self): |
| 1508 |
for key in self.iterator: |
| 1509 |
return self.space.newbytes(key) |
| 1510 |
else: |
| 1511 |
return None |
| 1512 |
|
| 1513 |
|
| 1514 |
class UnicodeIteratorImplementation(IteratorImplementation): |
| 1515 |
def __init__(self, space, strategy, w_set): |
| 1516 |
IteratorImplementation.__init__(self, space, strategy, w_set) |
| 1517 |
d = strategy.unerase(w_set.sstorage) |
| 1518 |
self.iterator = d.iterkeys() |
| 1519 |
|
| 1520 |
def next_entry(self): |
| 1521 |
for key in self.iterator: |
| 1522 |
return self.space.newutf8(key, len(key)) |
| 1523 |
else: |
| 1524 |
return None |
| 1525 |
|
| 1526 |
|
| 1527 |
class IntegerIteratorImplementation(IteratorImplementation): |
| 1528 |
|
| 1529 |
def __init__(self, space, strategy, w_set): |
| 1530 |
IteratorImplementation.__init__(self, space, strategy, w_set) |
| 1531 |
d = strategy.unerase(w_set.sstorage) |
| 1532 |
self.iterator = d.iterkeys() |
| 1533 |
|
| 1534 |
def next_entry(self): |
| 1535 |
|
| 1536 |
for key in self.iterator: |
| 1537 |
return self.space.newint(key) |
| 1538 |
else: |
| 1539 |
return None |
| 1540 |
|
| 1541 |
class IdentityIteratorImplementation(IteratorImplementation): |
| 1542 |
def __init__(self, space, strategy, w_set): |
| 1543 |
IteratorImplementation.__init__(self, space, strategy, w_set) |
| 1544 |
d = strategy.unerase(w_set.sstorage) |
| 1545 |
self.iterator = d.iterkeys() |
| 1546 |
|
| 1547 |
def next_entry(self): |
| 1548 |
for w_key in self.iterator: |
| 1549 |
return w_key |
| 1550 |
else: |
| 1551 |
return None |
| 1552 |
|
| 1553 |
class RDictIteratorImplementation(IteratorImplementation): |
| 1554 |
def __init__(self, space, strategy, w_set): |
| 1555 |
IteratorImplementation.__init__(self, space, strategy, w_set) |
| 1556 |
d = strategy.unerase(w_set.sstorage) |
| 1557 |
self.iterator = d.iterkeys() |
| 1558 |
|
| 1559 |
def next_entry(self): |
| 1560 |
|
| 1561 |
for w_key in self.iterator: |
| 1562 |
return w_key |
| 1563 |
else: |
| 1564 |
return None |
| 1565 |
|
| 1566 |
|
| 1567 |
class W_SetIterObject(W_Root): |
| 1568 |
|
| 1569 |
def __init__(self, space, iterimplementation): |
| 1570 |
self.space = space |
| 1571 |
self.iterimplementation = iterimplementation |
| 1572 |
|
| 1573 |
def descr_length_hint(self, space): |
| 1574 |
return space.newint(self.iterimplementation.length()) |
| 1575 |
|
| 1576 |
def descr_iter(self, space): |
| 1577 |
return self |
| 1578 |
|
| 1579 |
def descr_next(self, space): |
| 1580 |
iterimplementation = self.iterimplementation |
| 1581 |
w_key = iterimplementation.next() |
| 1582 |
if w_key is not None: |
| 1583 |
return w_key |
| 1584 |
raise OperationError(space.w_StopIteration, space.w_None) |
| 1585 |
|
| 1586 |
def descr_reduce(self, space): |
| 1587 |
|
| 1588 |
w_set = self.iterimplementation.setimplementation |
| 1589 |
w_clone = W_SetIterObject(space, w_set.iter()) |
| 1590 |
|
| 1591 |
for x in xrange(self.iterimplementation.pos): |
| 1592 |
w_clone.descr_next(space) |
| 1593 |
w_res = space.call_function(space.w_list, w_clone) |
| 1594 |
w_iter = space.builtin.get('iter') |
| 1595 |
return space.newtuple([w_iter, space.newtuple([w_res])]) |
| 1596 |
|
| 1597 |
|
| 1598 |
W_SetIterObject.typedef = TypeDef("setiterator", |
| 1599 |
__length_hint__ = gateway.interp2app(W_SetIterObject.descr_length_hint), |
| 1600 |
__iter__ = gateway.interp2app(W_SetIterObject.descr_iter), |
| 1601 |
__next__ = gateway.interp2app(W_SetIterObject.descr_next), |
| 1602 |
__reduce__ = gateway.interp2app(W_SetIterObject.descr_reduce), |
| 1603 |
) |
| 1604 |
setiter_typedef = W_SetIterObject.typedef |
| 1605 |
|
| 1606 |
|
| 1607 |
|
| 1608 |
|
| 1609 |
|
| 1610 |
def newset(space): |
| 1611 |
return r_dict(space.eq_w, space.hash_w, force_non_null=True) |
| 1612 |
|
| 1613 |
def set_strategy_and_setdata(space, w_set, w_iterable): |
| 1614 |
if w_iterable is None : |
| 1615 |
w_set.strategy = strategy = space.fromcache(EmptySetStrategy) |
| 1616 |
w_set.sstorage = strategy.get_empty_storage() |
| 1617 |
return |
| 1618 |
|
| 1619 |
if isinstance(w_iterable, W_BaseSetObject): |
| 1620 |
w_set.strategy = w_iterable.strategy |
| 1621 |
w_set.sstorage = w_iterable.get_storage_copy() |
| 1622 |
return |
| 1623 |
|
| 1624 |
byteslist = space.listview_bytes(w_iterable) |
| 1625 |
if byteslist is not None: |
| 1626 |
strategy = space.fromcache(BytesSetStrategy) |
| 1627 |
w_set.strategy = strategy |
| 1628 |
w_set.sstorage = strategy.get_storage_from_unwrapped_list(byteslist) |
| 1629 |
return |
| 1630 |
|
| 1631 |
unicodelist = space.listview_ascii(w_iterable) |
| 1632 |
if unicodelist is not None: |
| 1633 |
strategy = space.fromcache(AsciiSetStrategy) |
| 1634 |
w_set.strategy = strategy |
| 1635 |
w_set.sstorage = strategy.get_storage_from_unwrapped_list(unicodelist) |
| 1636 |
return |
| 1637 |
|
| 1638 |
intlist = space.listview_int(w_iterable) |
| 1639 |
if intlist is not None: |
| 1640 |
strategy = space.fromcache(IntegerSetStrategy) |
| 1641 |
w_set.strategy = strategy |
| 1642 |
w_set.sstorage = strategy.get_storage_from_unwrapped_list(intlist) |
| 1643 |
return |
| 1644 |
|
| 1645 |
length_hint = space.length_hint(w_iterable, 0) |
| 1646 |
|
| 1647 |
if jit.isconstant(length_hint) and length_hint: |
| 1648 |
return _pick_correct_strategy_unroll(space, w_set, w_iterable) |
| 1649 |
|
| 1650 |
w_set.strategy = strategy = space.fromcache(EmptySetStrategy) |
| 1651 |
w_set.sstorage = strategy.get_empty_storage() |
| 1652 |
_update_from_iterable(space, w_set, w_iterable) |
| 1653 |
|
| 1654 |
|
| 1655 |
@jit.unroll_safe |
| 1656 |
def _pick_correct_strategy_unroll(space, w_set, w_iterable): |
| 1657 |
|
| 1658 |
iterable_w = space.listview(w_iterable) |
| 1659 |
|
| 1660 |
for w_item in iterable_w: |
| 1661 |
if not is_plain_int1(w_item): |
| 1662 |
break |
| 1663 |
else: |
| 1664 |
w_set.strategy = space.fromcache(IntegerSetStrategy) |
| 1665 |
w_set.sstorage = w_set.strategy.get_storage_from_list(iterable_w) |
| 1666 |
return |
| 1667 |
|
| 1668 |
|
| 1669 |
for w_item in iterable_w: |
| 1670 |
if type(w_item) is not W_BytesObject: |
| 1671 |
break |
| 1672 |
else: |
| 1673 |
w_set.strategy = space.fromcache(BytesSetStrategy) |
| 1674 |
w_set.sstorage = w_set.strategy.get_storage_from_list(iterable_w) |
| 1675 |
return |
| 1676 |
|
| 1677 |
|
| 1678 |
for w_item in iterable_w: |
| 1679 |
if type(w_item) is not W_UnicodeObject or not w_item.is_ascii(): |
| 1680 |
break |
| 1681 |
else: |
| 1682 |
w_set.strategy = space.fromcache(AsciiSetStrategy) |
| 1683 |
w_set.sstorage = w_set.strategy.get_storage_from_list(iterable_w) |
| 1684 |
return |
| 1685 |
|
| 1686 |
|
| 1687 |
for w_item in iterable_w: |
| 1688 |
if not space.type(w_item).compares_by_identity(): |
| 1689 |
break |
| 1690 |
else: |
| 1691 |
w_set.strategy = space.fromcache(IdentitySetStrategy) |
| 1692 |
w_set.sstorage = w_set.strategy.get_storage_from_list(iterable_w) |
| 1693 |
return |
| 1694 |
|
| 1695 |
w_set.strategy = space.fromcache(ObjectSetStrategy) |
| 1696 |
w_set.sstorage = w_set.strategy.get_storage_from_list(iterable_w) |
| 1697 |
|
| 1698 |
|
| 1699 |
def get_printable_location(tp, strategy): |
| 1700 |
return "update_set: %s %s" % (tp.iterator_greenkey_printable(), strategy) |
| 1701 |
|
| 1702 |
update_set_driver = jit.JitDriver(name='update_set', |
| 1703 |
greens=['tp', 'strategy'], |
| 1704 |
reds='auto', |
| 1705 |
get_printable_location=get_printable_location) |
| 1706 |
|
| 1707 |
def _update_from_iterable(space, w_set, w_iterable): |
| 1708 |
tp = space.iterator_greenkey(w_iterable) |
| 1709 |
|
| 1710 |
w_iter = space.iter(w_iterable) |
| 1711 |
while True: |
| 1712 |
try: |
| 1713 |
w_item = space.next(w_iter) |
| 1714 |
except OperationError as e: |
| 1715 |
if not e.match(space, space.w_StopIteration): |
| 1716 |
raise |
| 1717 |
return |
| 1718 |
update_set_driver.jit_merge_point(tp=tp, strategy=w_set.strategy) |
| 1719 |
w_set.add(w_item) |
| 1720 |
|
| 1721 |
|
| 1722 |
def _initialize_set(space, w_obj, w_iterable=None): |
| 1723 |
w_obj.clear() |
| 1724 |
set_strategy_and_setdata(space, w_obj, w_iterable) |
| 1725 |
|
| 1726 |
def _convert_set_to_frozenset(space, w_obj): |
| 1727 |
if isinstance(w_obj, W_SetObject): |
| 1728 |
w_frozen = W_FrozensetObject(space, None) |
| 1729 |
w_frozen.strategy = w_obj.strategy |
| 1730 |
w_frozen.sstorage = w_obj.sstorage |
| 1731 |
return w_frozen |
| 1732 |
elif space.isinstance_w(w_obj, space.w_set): |
| 1733 |
w_frz = space.allocate_instance(W_FrozensetObject, space.w_frozenset) |
| 1734 |
W_FrozensetObject.__init__(w_frz, space, w_obj) |
| 1735 |
return w_frz |
| 1736 |
else: |
| 1737 |
return None |
| 1738 |
|
| 1739 |
|
| 1740 |
app = gateway.applevel(""" |
| 1741 |
def setrepr(currently_in_repr, s): |
| 1742 |
'The app-level part of repr().' |
| 1743 |
if s in currently_in_repr: |
| 1744 |
return '%s(...)' % (s.__class__.__name__,) |
| 1745 |
currently_in_repr[s] = 1 |
| 1746 |
try: |
| 1747 |
if not s: |
| 1748 |
return '%s()' % (s.__class__.__name__,) |
| 1749 |
listrepr = repr([x for x in s]) |
| 1750 |
if type(s) is set: |
| 1751 |
return '{%s}' % (listrepr[1:-1],) |
| 1752 |
else: |
| 1753 |
return '%s({%s})' % (s.__class__.__name__, listrepr[1:-1]) |
| 1754 |
finally: |
| 1755 |
try: |
| 1756 |
del currently_in_repr[s] |
| 1757 |
except: |
| 1758 |
pass |
| 1759 |
""", filename=__file__) |
| 1760 |
|
| 1761 |
setrepr = app.interphook("setrepr") |
| 1762 |
|
| 1763 |
app = gateway.applevel(""" |
| 1764 |
def setreduce(s): |
| 1765 |
return (s.__class__, (tuple(s),), s.__getstate__()) |
| 1766 |
|
| 1767 |
""", filename=__file__) |
| 1768 |
|
| 1769 |
setreduce = app.interphook('setreduce') |