Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1 | |
| 2 | /* Set object implementation using a hash table |
| 3 | Functions adapted from dictobject.c |
| 4 | */ |
| 5 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 6 | #include "Python.h" |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 7 | |
| 8 | /* This must be >= 1. */ |
| 9 | #define PERTURB_SHIFT 5 |
| 10 | |
| 11 | /* Object used as dummy key to fill deleted entries */ |
| 12 | static PyObject *dummy; /* Initialized by first call to make_new_set() */ |
| 13 | |
| 14 | #define EMPTY_TO_MINSIZE(so) do { \ |
| 15 | memset((so)->smalltable, 0, sizeof((so)->smalltable)); \ |
| 16 | (so)->used = (so)->fill = 0; \ |
| 17 | (so)->table = (so)->smalltable; \ |
| 18 | (so)->mask = PySet_MINSIZE - 1; \ |
| 19 | } while(0) |
| 20 | |
| 21 | |
| 22 | /* |
| 23 | The basic lookup function used by all operations. |
| 24 | This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4. |
| 25 | Open addressing is preferred over chaining since the link overhead for |
| 26 | chaining would be substantial (100% with typical malloc overhead). |
| 27 | |
| 28 | The initial probe index is computed as hash mod the table size. Subsequent |
| 29 | probe indices are computed as explained earlier. |
| 30 | |
| 31 | All arithmetic on hash should ignore overflow. |
| 32 | |
| 33 | This function must never return NULL; failures are indicated by returning |
| 34 | a setentry* for which the value field is NULL. Exceptions are never |
| 35 | reported by this function, and outstanding exceptions are maintained. |
| 36 | */ |
| 37 | |
| 38 | static setentry * |
| 39 | set_lookkey(PySetObject *so, PyObject *key, register long hash) |
| 40 | { |
| 41 | register int i; |
| 42 | register unsigned int perturb; |
| 43 | register setentry *freeslot; |
| 44 | register unsigned int mask = so->mask; |
| 45 | setentry *ep0 = so->table; |
| 46 | register setentry *ep; |
| 47 | register int restore_error; |
| 48 | register int checked_error; |
| 49 | register int cmp; |
| 50 | PyObject *err_type, *err_value, *err_tb; |
| 51 | PyObject *startkey; |
| 52 | |
| 53 | i = hash & mask; |
| 54 | ep = &ep0[i]; |
| 55 | if (ep->key == NULL || ep->key == key) |
| 56 | return ep; |
| 57 | |
| 58 | restore_error = checked_error = 0; |
| 59 | if (ep->key == dummy) |
| 60 | freeslot = ep; |
| 61 | else { |
| 62 | if (ep->hash == hash) { |
| 63 | /* error can't have been checked yet */ |
| 64 | checked_error = 1; |
| 65 | if (PyErr_Occurred()) { |
| 66 | restore_error = 1; |
| 67 | PyErr_Fetch(&err_type, &err_value, &err_tb); |
| 68 | } |
| 69 | startkey = ep->key; |
| 70 | cmp = PyObject_RichCompareBool(startkey, key, Py_EQ); |
| 71 | if (cmp < 0) |
| 72 | PyErr_Clear(); |
| 73 | if (ep0 == so->table && ep->key == startkey) { |
| 74 | if (cmp > 0) |
| 75 | goto Done; |
| 76 | } |
| 77 | else { |
| 78 | /* The compare did major nasty stuff to the |
| 79 | * set: start over. |
| 80 | */ |
| 81 | ep = set_lookkey(so, key, hash); |
| 82 | goto Done; |
| 83 | } |
| 84 | } |
| 85 | freeslot = NULL; |
| 86 | } |
| 87 | |
| 88 | /* In the loop, key == dummy is by far (factor of 100s) the |
| 89 | least likely outcome, so test for that last. */ |
| 90 | for (perturb = hash; ; perturb >>= PERTURB_SHIFT) { |
| 91 | i = (i << 2) + i + perturb + 1; |
| 92 | ep = &ep0[i & mask]; |
| 93 | if (ep->key == NULL) { |
| 94 | if (freeslot != NULL) |
| 95 | ep = freeslot; |
| 96 | break; |
| 97 | } |
| 98 | if (ep->key == key) |
| 99 | break; |
| 100 | if (ep->hash == hash && ep->key != dummy) { |
| 101 | if (!checked_error) { |
| 102 | checked_error = 1; |
| 103 | if (PyErr_Occurred()) { |
| 104 | restore_error = 1; |
| 105 | PyErr_Fetch(&err_type, &err_value, |
| 106 | &err_tb); |
| 107 | } |
| 108 | } |
| 109 | startkey = ep->key; |
| 110 | cmp = PyObject_RichCompareBool(startkey, key, Py_EQ); |
| 111 | if (cmp < 0) |
| 112 | PyErr_Clear(); |
| 113 | if (ep0 == so->table && ep->key == startkey) { |
| 114 | if (cmp > 0) |
| 115 | break; |
| 116 | } |
| 117 | else { |
| 118 | /* The compare did major nasty stuff to the |
| 119 | * set: start over. |
| 120 | */ |
| 121 | ep = set_lookkey(so, key, hash); |
| 122 | break; |
| 123 | } |
| 124 | } |
| 125 | else if (ep->key == dummy && freeslot == NULL) |
| 126 | freeslot = ep; |
| 127 | } |
| 128 | |
| 129 | Done: |
| 130 | if (restore_error) |
| 131 | PyErr_Restore(err_type, err_value, err_tb); |
| 132 | return ep; |
| 133 | } |
| 134 | |
| 135 | /* |
| 136 | * Hacked up version of set_lookkey which can assume keys are always strings; |
| 137 | * this assumption allows testing for errors during PyObject_Compare() to |
| 138 | * be dropped; string-string comparisons never raise exceptions. This also |
| 139 | * means we don't need to go through PyObject_Compare(); we can always use |
| 140 | * _PyString_Eq directly. |
| 141 | * |
| 142 | * This is valuable because the general-case error handling in set_lookkey() is |
| 143 | * expensive, and sets with pure-string keys may be very common. |
| 144 | */ |
| 145 | static setentry * |
| 146 | set_lookkey_string(PySetObject *so, PyObject *key, register long hash) |
| 147 | { |
| 148 | register int i; |
| 149 | register unsigned int perturb; |
| 150 | register setentry *freeslot; |
| 151 | register unsigned int mask = so->mask; |
| 152 | setentry *ep0 = so->table; |
| 153 | register setentry *ep; |
| 154 | |
| 155 | /* Make sure this function doesn't have to handle non-string keys, |
| 156 | including subclasses of str; e.g., one reason to subclass |
| 157 | strings is to override __eq__, and for speed we don't cater to |
| 158 | that here. */ |
| 159 | if (!PyString_CheckExact(key)) { |
| 160 | so->lookup = set_lookkey; |
| 161 | return set_lookkey(so, key, hash); |
| 162 | } |
| 163 | i = hash & mask; |
| 164 | ep = &ep0[i]; |
| 165 | if (ep->key == NULL || ep->key == key) |
| 166 | return ep; |
| 167 | if (ep->key == dummy) |
| 168 | freeslot = ep; |
| 169 | else { |
| 170 | if (ep->hash == hash && _PyString_Eq(ep->key, key)) |
| 171 | return ep; |
| 172 | freeslot = NULL; |
| 173 | } |
| 174 | |
| 175 | /* In the loop, key == dummy is by far (factor of 100s) the |
| 176 | least likely outcome, so test for that last. */ |
| 177 | for (perturb = hash; ; perturb >>= PERTURB_SHIFT) { |
| 178 | i = (i << 2) + i + perturb + 1; |
| 179 | ep = &ep0[i & mask]; |
| 180 | if (ep->key == NULL) |
| 181 | return freeslot == NULL ? ep : freeslot; |
| 182 | if (ep->key == key |
| 183 | || (ep->hash == hash |
| 184 | && ep->key != dummy |
| 185 | && _PyString_Eq(ep->key, key))) |
| 186 | return ep; |
| 187 | if (ep->key == dummy && freeslot == NULL) |
| 188 | freeslot = ep; |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | /* |
| 193 | Internal routine to insert a new item into the table. |
| 194 | Used both by the internal resize routine and by the public insert routine. |
| 195 | Eats a reference to key. |
| 196 | */ |
| 197 | static void |
| 198 | set_insert_key(register PySetObject *so, PyObject *key, long hash) |
| 199 | { |
| 200 | register setentry *ep; |
| 201 | typedef setentry *(*lookupfunc)(PySetObject *, PyObject *, long); |
| 202 | |
| 203 | assert(so->lookup != NULL); |
| 204 | |
| 205 | ep = so->lookup(so, key, hash); |
| 206 | if (ep->key == NULL) { |
| 207 | /* UNUSED */ |
| 208 | so->fill++; |
| 209 | ep->key = key; |
| 210 | ep->hash = hash; |
| 211 | so->used++; |
| 212 | } else if (ep->key == dummy) { |
| 213 | /* DUMMY */ |
| 214 | ep->key = key; |
| 215 | ep->hash = hash; |
| 216 | so->used++; |
| 217 | Py_DECREF(dummy); |
| 218 | } else { |
| 219 | /* ACTIVE */ |
| 220 | Py_DECREF(key); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | /* |
| 225 | Restructure the table by allocating a new table and reinserting all |
| 226 | items again. When entries have been deleted, the new table may |
| 227 | actually be smaller than the old one. |
| 228 | */ |
| 229 | static int |
| 230 | set_table_resize(PySetObject *so, int minused) |
| 231 | { |
| 232 | int newsize; |
| 233 | setentry *oldtable, *newtable, *ep; |
| 234 | int i; |
| 235 | int is_oldtable_malloced; |
| 236 | setentry small_copy[PySet_MINSIZE]; |
| 237 | |
| 238 | assert(minused >= 0); |
| 239 | |
| 240 | /* Find the smallest table size > minused. */ |
| 241 | for (newsize = PySet_MINSIZE; |
| 242 | newsize <= minused && newsize > 0; |
| 243 | newsize <<= 1) |
| 244 | ; |
| 245 | if (newsize <= 0) { |
| 246 | PyErr_NoMemory(); |
| 247 | return -1; |
| 248 | } |
| 249 | |
| 250 | /* Get space for a new table. */ |
| 251 | oldtable = so->table; |
| 252 | assert(oldtable != NULL); |
| 253 | is_oldtable_malloced = oldtable != so->smalltable; |
| 254 | |
| 255 | if (newsize == PySet_MINSIZE) { |
| 256 | /* A large table is shrinking, or we can't get any smaller. */ |
| 257 | newtable = so->smalltable; |
| 258 | if (newtable == oldtable) { |
| 259 | if (so->fill == so->used) { |
| 260 | /* No dummies, so no point doing anything. */ |
| 261 | return 0; |
| 262 | } |
| 263 | /* We're not going to resize it, but rebuild the |
| 264 | table anyway to purge old dummy entries. |
| 265 | Subtle: This is *necessary* if fill==size, |
| 266 | as set_lookkey needs at least one virgin slot to |
| 267 | terminate failing searches. If fill < size, it's |
| 268 | merely desirable, as dummies slow searches. */ |
| 269 | assert(so->fill > so->used); |
| 270 | memcpy(small_copy, oldtable, sizeof(small_copy)); |
| 271 | oldtable = small_copy; |
| 272 | } |
| 273 | } |
| 274 | else { |
| 275 | newtable = PyMem_NEW(setentry, newsize); |
| 276 | if (newtable == NULL) { |
| 277 | PyErr_NoMemory(); |
| 278 | return -1; |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | /* Make the set empty, using the new table. */ |
| 283 | assert(newtable != oldtable); |
| 284 | so->table = newtable; |
| 285 | so->mask = newsize - 1; |
| 286 | memset(newtable, 0, sizeof(setentry) * newsize); |
| 287 | so->used = 0; |
| 288 | i = so->fill; |
| 289 | so->fill = 0; |
| 290 | |
| 291 | /* Copy the data over; this is refcount-neutral for active entries; |
| 292 | dummy entries aren't copied over, of course */ |
| 293 | for (ep = oldtable; i > 0; ep++) { |
| 294 | if (ep->key == NULL) { |
| 295 | /* UNUSED */ |
| 296 | ; |
| 297 | } else if (ep->key == dummy) { |
| 298 | /* DUMMY */ |
| 299 | --i; |
| 300 | assert(ep->key == dummy); |
| 301 | Py_DECREF(ep->key); |
| 302 | } else { |
| 303 | /* ACTIVE */ |
| 304 | --i; |
| 305 | set_insert_key(so, ep->key, ep->hash); |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | if (is_oldtable_malloced) |
| 310 | PyMem_DEL(oldtable); |
| 311 | return 0; |
| 312 | } |
| 313 | |
| 314 | /*** Internal functions (derived from public dictionary api functions ) ***/ |
| 315 | |
| 316 | |
| 317 | /* CAUTION: set_add_internal() must guarantee that it won't resize the table */ |
| 318 | static int |
| 319 | set_add_internal(register PySetObject *so, PyObject *key) |
| 320 | { |
| 321 | register long hash; |
| 322 | register int n_used; |
| 323 | |
| 324 | if (PyString_CheckExact(key)) { |
| 325 | hash = ((PyStringObject *)key)->ob_shash; |
| 326 | if (hash == -1) |
| 327 | hash = PyObject_Hash(key); |
| 328 | } else { |
| 329 | hash = PyObject_Hash(key); |
| 330 | if (hash == -1) |
| 331 | return -1; |
| 332 | } |
| 333 | assert(so->fill <= so->mask); /* at least one empty slot */ |
| 334 | n_used = so->used; |
| 335 | Py_INCREF(key); |
| 336 | set_insert_key(so, key, hash); |
| 337 | if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2)) |
| 338 | return 0; |
| 339 | return set_table_resize(so, so->used*(so->used>50000 ? 2 : 4)); |
| 340 | } |
| 341 | |
| 342 | #define DISCARD_NOTFOUND 0 |
| 343 | #define DISCARD_FOUND 1 |
| 344 | |
| 345 | static int |
| 346 | set_discard_internal(PySetObject *so, PyObject *key) |
| 347 | { |
| 348 | register long hash; |
| 349 | register setentry *ep; |
| 350 | PyObject *old_key; |
| 351 | |
| 352 | assert (PyAnySet_Check(so)); |
| 353 | if (!PyString_CheckExact(key) || |
| 354 | (hash = ((PyStringObject *) key)->ob_shash) == -1) { |
| 355 | hash = PyObject_Hash(key); |
| 356 | if (hash == -1) |
| 357 | return -1; |
| 358 | } |
| 359 | ep = (so->lookup)(so, key, hash); |
| 360 | if (ep->key == NULL || ep->key == dummy) |
| 361 | return DISCARD_NOTFOUND; |
| 362 | old_key = ep->key; |
| 363 | Py_INCREF(dummy); |
| 364 | ep->key = dummy; |
| 365 | so->used--; |
| 366 | Py_DECREF(old_key); |
| 367 | return DISCARD_FOUND; |
| 368 | } |
| 369 | |
| 370 | static void |
| 371 | set_clear_internal(PySetObject *so) |
| 372 | { |
| 373 | setentry *ep, *table; |
| 374 | int table_is_malloced; |
| 375 | int fill; |
| 376 | setentry small_copy[PySet_MINSIZE]; |
| 377 | #ifdef Py_DEBUG |
| 378 | int i, n; |
| 379 | #endif |
| 380 | |
| 381 | assert (PyAnySet_Check(so)); |
| 382 | #ifdef Py_DEBUG |
| 383 | n = so->mask + 1; |
| 384 | i = 0; |
| 385 | #endif |
| 386 | |
| 387 | table = so->table; |
| 388 | assert(table != NULL); |
| 389 | table_is_malloced = table != so->smalltable; |
| 390 | |
| 391 | /* This is delicate. During the process of clearing the set, |
| 392 | * decrefs can cause the set to mutate. To avoid fatal confusion |
| 393 | * (voice of experience), we have to make the set empty before |
| 394 | * clearing the slots, and never refer to anything via mp->ref while |
| 395 | * clearing. |
| 396 | */ |
| 397 | fill = so->fill; |
| 398 | if (table_is_malloced) |
| 399 | EMPTY_TO_MINSIZE(so); |
| 400 | |
| 401 | else if (fill > 0) { |
| 402 | /* It's a small table with something that needs to be cleared. |
| 403 | * Afraid the only safe way is to copy the set entries into |
| 404 | * another small table first. |
| 405 | */ |
| 406 | memcpy(small_copy, table, sizeof(small_copy)); |
| 407 | table = small_copy; |
| 408 | EMPTY_TO_MINSIZE(so); |
| 409 | } |
| 410 | /* else it's a small table that's already empty */ |
| 411 | |
| 412 | /* Now we can finally clear things. If C had refcounts, we could |
| 413 | * assert that the refcount on table is 1 now, i.e. that this function |
| 414 | * has unique access to it, so decref side-effects can't alter it. |
| 415 | */ |
| 416 | for (ep = table; fill > 0; ++ep) { |
| 417 | #ifdef Py_DEBUG |
| 418 | assert(i < n); |
| 419 | ++i; |
| 420 | #endif |
| 421 | if (ep->key) { |
| 422 | --fill; |
| 423 | Py_DECREF(ep->key); |
| 424 | } |
| 425 | #ifdef Py_DEBUG |
| 426 | else |
| 427 | assert(ep->key == NULL || ep->key == dummy); |
| 428 | #endif |
| 429 | } |
| 430 | |
| 431 | if (table_is_malloced) |
| 432 | PyMem_DEL(table); |
| 433 | } |
| 434 | |
| 435 | /* |
| 436 | * Iterate over a set table. Use like so: |
| 437 | * |
| 438 | * int i; |
| 439 | * PyObject *key; |
| 440 | * i = 0; # important! i should not otherwise be changed by you |
| 441 | * while (set_next_internal(yourset, &i, &key)) { |
| 442 | * Refer to borrowed reference in key. |
| 443 | * } |
| 444 | * |
| 445 | * CAUTION: In general, it isn't safe to use set_next_internal in a loop that |
| 446 | * mutates the table. |
| 447 | */ |
| 448 | static int |
| 449 | set_next_internal(PySetObject *so, int *ppos, PyObject **pkey) |
| 450 | { |
| 451 | register int i, mask; |
| 452 | register setentry *ep; |
| 453 | |
| 454 | assert (PyAnySet_Check(so)); |
| 455 | i = *ppos; |
| 456 | if (i < 0) |
| 457 | return 0; |
| 458 | ep = so->table; |
| 459 | mask = so->mask; |
| 460 | while (i <= mask && (ep[i].key == NULL || ep[i].key == dummy)) |
| 461 | i++; |
| 462 | *ppos = i+1; |
| 463 | if (i > mask) |
| 464 | return 0; |
| 465 | if (pkey) |
| 466 | *pkey = ep[i].key; |
| 467 | return 1; |
| 468 | } |
| 469 | |
| 470 | /* Methods */ |
| 471 | |
| 472 | static int |
| 473 | set_merge_internal(PySetObject *so, PyObject *b) |
| 474 | { |
| 475 | register PySetObject *other; |
| 476 | register int i; |
| 477 | setentry *entry; |
| 478 | |
| 479 | assert (PyAnySet_Check(so)); |
| 480 | assert (PyAnySet_Check(b)); |
| 481 | |
| 482 | other = (PySetObject*)b; |
| 483 | if (other == so || other->used == 0) |
| 484 | /* a.update(a) or a.update({}); nothing to do */ |
| 485 | return 0; |
| 486 | /* Do one big resize at the start, rather than |
| 487 | * incrementally resizing as we insert new items. Expect |
| 488 | * that there will be no (or few) overlapping keys. |
| 489 | */ |
| 490 | if ((so->fill + other->used)*3 >= (so->mask+1)*2) { |
| 491 | if (set_table_resize(so, (so->used + other->used)*2) != 0) |
| 492 | return -1; |
| 493 | } |
| 494 | for (i = 0; i <= other->mask; i++) { |
| 495 | entry = &other->table[i]; |
| 496 | if (entry->key != NULL && |
| 497 | entry->key != dummy) { |
| 498 | Py_INCREF(entry->key); |
| 499 | set_insert_key(so, entry->key, entry->hash); |
| 500 | } |
| 501 | } |
| 502 | return 0; |
| 503 | } |
| 504 | |
| 505 | static int |
| 506 | set_contains_internal(PySetObject *so, PyObject *key) |
| 507 | { |
| 508 | long hash; |
| 509 | |
| 510 | if (!PyString_CheckExact(key) || |
| 511 | (hash = ((PyStringObject *) key)->ob_shash) == -1) { |
| 512 | hash = PyObject_Hash(key); |
| 513 | if (hash == -1) |
| 514 | return -1; |
| 515 | } |
| 516 | key = (so->lookup)(so, key, hash)->key; |
| 517 | return key != NULL && key != dummy; |
| 518 | } |
| 519 | |
| 520 | static PyTypeObject PySetIter_Type; /* Forward */ |
| 521 | |
| 522 | /* Set iterator types */ |
| 523 | |
| 524 | typedef struct { |
| 525 | PyObject_HEAD |
| 526 | PySetObject *si_set; /* Set to NULL when iterator is exhausted */ |
| 527 | int si_used; |
| 528 | int si_pos; |
| 529 | long len; |
| 530 | } setiterobject; |
| 531 | |
| 532 | static PyObject * |
| 533 | set_iter(PySetObject *so) |
| 534 | { |
| 535 | setiterobject *si = PyObject_New(setiterobject, &PySetIter_Type); |
| 536 | if (si == NULL) |
| 537 | return NULL; |
| 538 | Py_INCREF(so); |
| 539 | si->si_set = so; |
| 540 | si->si_used = so->used; |
| 541 | si->si_pos = 0; |
| 542 | si->len = so->used; |
| 543 | return (PyObject *)si; |
| 544 | } |
| 545 | |
| 546 | static void |
| 547 | setiter_dealloc(setiterobject *si) |
| 548 | { |
| 549 | Py_XDECREF(si->si_set); |
| 550 | PyObject_Del(si); |
| 551 | } |
| 552 | |
| 553 | static int |
| 554 | setiter_len(setiterobject *si) |
| 555 | { |
| 556 | if (si->si_set != NULL && si->si_used == si->si_set->used) |
| 557 | return si->len; |
| 558 | return 0; |
| 559 | } |
| 560 | |
| 561 | static PySequenceMethods setiter_as_sequence = { |
| 562 | (inquiry)setiter_len, /* sq_length */ |
| 563 | 0, /* sq_concat */ |
| 564 | }; |
| 565 | |
| 566 | static PyObject *setiter_iternextkey(setiterobject *si) |
| 567 | { |
| 568 | PyObject *key; |
| 569 | register int i, mask; |
| 570 | register setentry *ep; |
| 571 | PySetObject *d = si->si_set; |
| 572 | |
| 573 | if (d == NULL) |
| 574 | return NULL; |
| 575 | assert (PyAnySet_Check(d)); |
| 576 | |
| 577 | if (si->si_used != d->used) { |
| 578 | PyErr_SetString(PyExc_RuntimeError, |
| 579 | "Set changed size during iteration"); |
| 580 | si->si_used = -1; /* Make this state sticky */ |
| 581 | return NULL; |
| 582 | } |
| 583 | |
| 584 | i = si->si_pos; |
| 585 | if (i < 0) |
| 586 | goto fail; |
| 587 | ep = d->table; |
| 588 | mask = d->mask; |
| 589 | while (i <= mask && (ep[i].key == NULL || ep[i].key == dummy)) |
| 590 | i++; |
| 591 | si->si_pos = i+1; |
| 592 | if (i > mask) |
| 593 | goto fail; |
| 594 | si->len--; |
| 595 | key = ep[i].key; |
| 596 | Py_INCREF(key); |
| 597 | return key; |
| 598 | |
| 599 | fail: |
| 600 | Py_DECREF(d); |
| 601 | si->si_set = NULL; |
| 602 | return NULL; |
| 603 | } |
| 604 | |
| 605 | PyTypeObject PySetIter_Type = { |
| 606 | PyObject_HEAD_INIT(&PyType_Type) |
| 607 | 0, /* ob_size */ |
| 608 | "Set-keyiterator", /* tp_name */ |
| 609 | sizeof(setiterobject), /* tp_basicsize */ |
| 610 | 0, /* tp_itemsize */ |
| 611 | /* methods */ |
| 612 | (destructor)setiter_dealloc, /* tp_dealloc */ |
| 613 | 0, /* tp_print */ |
| 614 | 0, /* tp_getattr */ |
| 615 | 0, /* tp_setattr */ |
| 616 | 0, /* tp_compare */ |
| 617 | 0, /* tp_repr */ |
| 618 | 0, /* tp_as_number */ |
| 619 | &setiter_as_sequence, /* tp_as_sequence */ |
| 620 | 0, /* tp_as_mapping */ |
| 621 | 0, /* tp_hash */ |
| 622 | 0, /* tp_call */ |
| 623 | 0, /* tp_str */ |
| 624 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 625 | 0, /* tp_setattro */ |
| 626 | 0, /* tp_as_buffer */ |
| 627 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 628 | 0, /* tp_doc */ |
| 629 | 0, /* tp_traverse */ |
| 630 | 0, /* tp_clear */ |
| 631 | 0, /* tp_richcompare */ |
| 632 | 0, /* tp_weaklistoffset */ |
| 633 | PyObject_SelfIter, /* tp_iter */ |
| 634 | (iternextfunc)setiter_iternextkey, /* tp_iternext */ |
| 635 | }; |
| 636 | |
| 637 | /***** Derived functions (table accesses only done with above primitives *****/ |
| 638 | |
Raymond Hettinger | 691d805 | 2004-05-30 07:26:47 +0000 | [diff] [blame] | 639 | #include "structmember.h" |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 640 | |
| 641 | /* set object implementation |
| 642 | written and maintained by Raymond D. Hettinger <python@rcn.com> |
| 643 | derived from sets.py written by Greg V. Wilson, Alex Martelli, |
| 644 | Guido van Rossum, Raymond Hettinger, and Tim Peters. |
| 645 | |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 646 | Copyright (c) 2003-5 Python Software Foundation. |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 647 | All rights reserved. |
| 648 | */ |
| 649 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 650 | static PyObject * |
Raymond Hettinger | a38123e | 2003-11-24 22:18:49 +0000 | [diff] [blame] | 651 | set_update(PySetObject *so, PyObject *other) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 652 | { |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 653 | PyObject *item, *it; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 654 | |
Raymond Hettinger | a38123e | 2003-11-24 22:18:49 +0000 | [diff] [blame] | 655 | if (PyAnySet_Check(other)) { |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 656 | if (set_merge_internal(so, other) == -1) |
Raymond Hettinger | a38123e | 2003-11-24 22:18:49 +0000 | [diff] [blame] | 657 | return NULL; |
| 658 | Py_RETURN_NONE; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 659 | } |
| 660 | |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 661 | if (PyDict_Check(other)) { |
| 662 | PyObject *value, *item; |
| 663 | int pos = 0; |
| 664 | while (PyDict_Next(other, &pos, &item, &value)) { |
| 665 | if (set_add_internal(so, item) == -1) |
| 666 | return NULL; |
| 667 | } |
| 668 | Py_RETURN_NONE; |
| 669 | } |
| 670 | |
Raymond Hettinger | a38123e | 2003-11-24 22:18:49 +0000 | [diff] [blame] | 671 | it = PyObject_GetIter(other); |
| 672 | if (it == NULL) |
| 673 | return NULL; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 674 | |
Raymond Hettinger | a38123e | 2003-11-24 22:18:49 +0000 | [diff] [blame] | 675 | while ((item = PyIter_Next(it)) != NULL) { |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 676 | if (set_add_internal(so, item) == -1) { |
Raymond Hettinger | a38123e | 2003-11-24 22:18:49 +0000 | [diff] [blame] | 677 | Py_DECREF(it); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 678 | Py_DECREF(item); |
Raymond Hettinger | a38123e | 2003-11-24 22:18:49 +0000 | [diff] [blame] | 679 | return NULL; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 680 | } |
| 681 | Py_DECREF(item); |
| 682 | } |
Raymond Hettinger | a38123e | 2003-11-24 22:18:49 +0000 | [diff] [blame] | 683 | Py_DECREF(it); |
Raymond Hettinger | f5f41bf | 2003-11-24 02:57:33 +0000 | [diff] [blame] | 684 | if (PyErr_Occurred()) |
Raymond Hettinger | a38123e | 2003-11-24 22:18:49 +0000 | [diff] [blame] | 685 | return NULL; |
| 686 | Py_RETURN_NONE; |
| 687 | } |
| 688 | |
| 689 | PyDoc_STRVAR(update_doc, |
| 690 | "Update a set with the union of itself and another."); |
| 691 | |
| 692 | static PyObject * |
| 693 | make_new_set(PyTypeObject *type, PyObject *iterable) |
| 694 | { |
Raymond Hettinger | a38123e | 2003-11-24 22:18:49 +0000 | [diff] [blame] | 695 | PyObject *tmp; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 696 | register PySetObject *so = NULL; |
Raymond Hettinger | a38123e | 2003-11-24 22:18:49 +0000 | [diff] [blame] | 697 | |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 698 | if (dummy == NULL) { /* Auto-initialize dummy */ |
| 699 | dummy = PyString_FromString("<dummy key>"); |
| 700 | if (dummy == NULL) |
| 701 | return NULL; |
| 702 | } |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 703 | |
| 704 | /* create PySetObject structure */ |
| 705 | so = (PySetObject *)type->tp_alloc(type, 0); |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 706 | if (so == NULL) |
Raymond Hettinger | a38123e | 2003-11-24 22:18:49 +0000 | [diff] [blame] | 707 | return NULL; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 708 | |
| 709 | EMPTY_TO_MINSIZE(so); |
| 710 | so->lookup = set_lookkey_string; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 711 | so->hash = -1; |
Raymond Hettinger | 691d805 | 2004-05-30 07:26:47 +0000 | [diff] [blame] | 712 | so->weakreflist = NULL; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 713 | |
Raymond Hettinger | a38123e | 2003-11-24 22:18:49 +0000 | [diff] [blame] | 714 | if (iterable != NULL) { |
| 715 | tmp = set_update(so, iterable); |
| 716 | if (tmp == NULL) { |
| 717 | Py_DECREF(so); |
| 718 | return NULL; |
| 719 | } |
| 720 | Py_DECREF(tmp); |
| 721 | } |
| 722 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 723 | return (PyObject *)so; |
| 724 | } |
| 725 | |
| 726 | static PyObject * |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 727 | frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 728 | { |
| 729 | PyObject *iterable = NULL; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 730 | static PyObject *emptyfrozenset = NULL; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 731 | |
| 732 | if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable)) |
| 733 | return NULL; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 734 | if (iterable == NULL) { |
| 735 | if (type == &PyFrozenSet_Type) { |
| 736 | if (emptyfrozenset == NULL) |
| 737 | emptyfrozenset = make_new_set(type, NULL); |
| 738 | else |
| 739 | Py_INCREF(emptyfrozenset); |
| 740 | return emptyfrozenset; |
| 741 | } |
| 742 | } else if (PyFrozenSet_CheckExact(iterable)) { |
Raymond Hettinger | 49ba4c3 | 2003-11-23 02:49:05 +0000 | [diff] [blame] | 743 | Py_INCREF(iterable); |
| 744 | return iterable; |
| 745 | } |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 746 | return make_new_set(type, iterable); |
| 747 | } |
| 748 | |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 749 | static PyObject * |
| 750 | set_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 751 | { |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 752 | return make_new_set(type, NULL); |
| 753 | } |
| 754 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 755 | static void |
| 756 | set_dealloc(PySetObject *so) |
| 757 | { |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 758 | register setentry *ep; |
| 759 | int fill = so->fill; |
| 760 | |
Raymond Hettinger | bb999b5 | 2005-06-18 21:00:26 +0000 | [diff] [blame] | 761 | PyObject_GC_UnTrack(so); |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 762 | Py_TRASHCAN_SAFE_BEGIN(so) |
Raymond Hettinger | 691d805 | 2004-05-30 07:26:47 +0000 | [diff] [blame] | 763 | if (so->weakreflist != NULL) |
| 764 | PyObject_ClearWeakRefs((PyObject *) so); |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 765 | |
| 766 | for (ep = so->table; fill > 0; ep++) { |
| 767 | if (ep->key) { |
| 768 | --fill; |
| 769 | Py_DECREF(ep->key); |
| 770 | } |
| 771 | } |
| 772 | if (so->table != so->smalltable) |
| 773 | PyMem_DEL(so->table); |
| 774 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 775 | so->ob_type->tp_free(so); |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 776 | Py_TRASHCAN_SAFE_END(so) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 777 | } |
| 778 | |
Raymond Hettinger | bb999b5 | 2005-06-18 21:00:26 +0000 | [diff] [blame] | 779 | static int |
| 780 | set_traverse(PySetObject *so, visitproc visit, void *arg) |
| 781 | { |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 782 | int i = 0; |
| 783 | PyObject *pk; |
| 784 | |
| 785 | while (set_next_internal(so, &i, &pk)) |
| 786 | Py_VISIT(pk); |
Raymond Hettinger | bb999b5 | 2005-06-18 21:00:26 +0000 | [diff] [blame] | 787 | return 0; |
| 788 | } |
| 789 | |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 790 | static int |
| 791 | set_len(PyObject *so) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 792 | { |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 793 | return ((PySetObject *)so)->used; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 794 | } |
| 795 | |
Raymond Hettinger | 934d63e | 2005-07-31 01:33:10 +0000 | [diff] [blame^] | 796 | /* set_swap_bodies() switches the contents of any two sets by moving their |
| 797 | internal data pointers and, if needed, copying the internal smalltables. |
| 798 | Semantically equivalent to: |
| 799 | |
| 800 | t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t |
| 801 | |
| 802 | The function always succeeds and it leaves both objects in a stable state. |
| 803 | Useful for creating temporary frozensets from sets for membership testing |
| 804 | in __contains__(), discard(), and remove(). Also useful for operations |
| 805 | that update in-place (by allowing an intermediate result to be swapped |
| 806 | into one of original the inputs). |
| 807 | */ |
| 808 | |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 809 | static void |
| 810 | set_swap_bodies(PySetObject *a, PySetObject *b) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 811 | { |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 812 | int t; |
| 813 | setentry *u; |
| 814 | setentry *(*f)(PySetObject *so, PyObject *key, long hash); |
| 815 | setentry tab[PySet_MINSIZE]; |
| 816 | long h; |
| 817 | |
| 818 | t = a->fill; a->fill = b->fill; b->fill = t; |
| 819 | t = a->used; a->used = b->used; b->used = t; |
| 820 | t = a->mask; a->mask = b->mask; b->mask = t; |
| 821 | |
| 822 | u = a->table; |
| 823 | if (a->table == a->smalltable) |
| 824 | u = b->smalltable; |
| 825 | a->table = b->table; |
| 826 | if (b->table == b->smalltable) |
| 827 | a->table = a->smalltable; |
| 828 | b->table = u; |
| 829 | |
| 830 | f = a->lookup; a->lookup = b->lookup; b->lookup = f; |
| 831 | |
| 832 | if (a->table == a->smalltable || b->table == b->smalltable) { |
| 833 | memcpy(tab, a->smalltable, sizeof(tab)); |
| 834 | memcpy(a->smalltable, b->smalltable, sizeof(tab)); |
| 835 | memcpy(b->smalltable, tab, sizeof(tab)); |
| 836 | } |
| 837 | |
| 838 | h = a->hash; a->hash = b->hash; b->hash = h; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 839 | } |
| 840 | |
| 841 | static int |
| 842 | set_contains(PySetObject *so, PyObject *key) |
| 843 | { |
Raymond Hettinger | bfd334a | 2003-11-22 03:55:23 +0000 | [diff] [blame] | 844 | PyObject *tmp; |
Raymond Hettinger | 19c2d77 | 2003-11-21 18:36:54 +0000 | [diff] [blame] | 845 | int result; |
| 846 | |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 847 | result = set_contains_internal(so, key); |
Raymond Hettinger | a38123e | 2003-11-24 22:18:49 +0000 | [diff] [blame] | 848 | if (result == -1 && PyAnySet_Check(key)) { |
Raymond Hettinger | 19c2d77 | 2003-11-21 18:36:54 +0000 | [diff] [blame] | 849 | PyErr_Clear(); |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 850 | tmp = make_new_set(&PyFrozenSet_Type, NULL); |
Raymond Hettinger | 19c2d77 | 2003-11-21 18:36:54 +0000 | [diff] [blame] | 851 | if (tmp == NULL) |
| 852 | return -1; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 853 | set_swap_bodies((PySetObject *)tmp, (PySetObject *)key); |
| 854 | result = set_contains_internal(so, tmp); |
| 855 | set_swap_bodies((PySetObject *)tmp, (PySetObject *)key); |
Raymond Hettinger | 19c2d77 | 2003-11-21 18:36:54 +0000 | [diff] [blame] | 856 | Py_DECREF(tmp); |
| 857 | } |
| 858 | return result; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 859 | } |
| 860 | |
| 861 | static PyObject * |
Raymond Hettinger | 8f5cdaa | 2003-12-13 11:26:12 +0000 | [diff] [blame] | 862 | set_direct_contains(PySetObject *so, PyObject *key) |
| 863 | { |
Raymond Hettinger | 8f5cdaa | 2003-12-13 11:26:12 +0000 | [diff] [blame] | 864 | long result; |
| 865 | |
Raymond Hettinger | 438e02d | 2003-12-13 19:38:47 +0000 | [diff] [blame] | 866 | result = set_contains(so, key); |
Raymond Hettinger | 8f5cdaa | 2003-12-13 11:26:12 +0000 | [diff] [blame] | 867 | if (result == -1) |
| 868 | return NULL; |
| 869 | return PyBool_FromLong(result); |
| 870 | } |
| 871 | |
| 872 | PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x."); |
| 873 | |
| 874 | static PyObject * |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 875 | set_copy(PySetObject *so) |
| 876 | { |
Raymond Hettinger | a38123e | 2003-11-24 22:18:49 +0000 | [diff] [blame] | 877 | return make_new_set(so->ob_type, (PyObject *)so); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 878 | } |
| 879 | |
Raymond Hettinger | 49ba4c3 | 2003-11-23 02:49:05 +0000 | [diff] [blame] | 880 | static PyObject * |
| 881 | frozenset_copy(PySetObject *so) |
| 882 | { |
Raymond Hettinger | f5f41bf | 2003-11-24 02:57:33 +0000 | [diff] [blame] | 883 | if (PyFrozenSet_CheckExact(so)) { |
Raymond Hettinger | 49ba4c3 | 2003-11-23 02:49:05 +0000 | [diff] [blame] | 884 | Py_INCREF(so); |
| 885 | return (PyObject *)so; |
| 886 | } |
| 887 | return set_copy(so); |
| 888 | } |
| 889 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 890 | PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set."); |
| 891 | |
| 892 | static PyObject * |
Raymond Hettinger | f5f41bf | 2003-11-24 02:57:33 +0000 | [diff] [blame] | 893 | set_union(PySetObject *so, PyObject *other) |
| 894 | { |
| 895 | PySetObject *result; |
| 896 | PyObject *rv; |
| 897 | |
| 898 | result = (PySetObject *)set_copy(so); |
| 899 | if (result == NULL) |
| 900 | return NULL; |
Raymond Hettinger | a38123e | 2003-11-24 22:18:49 +0000 | [diff] [blame] | 901 | rv = set_update(result, other); |
Raymond Hettinger | f5f41bf | 2003-11-24 02:57:33 +0000 | [diff] [blame] | 902 | if (rv == NULL) { |
| 903 | Py_DECREF(result); |
| 904 | return NULL; |
| 905 | } |
| 906 | Py_DECREF(rv); |
| 907 | return (PyObject *)result; |
| 908 | } |
| 909 | |
| 910 | PyDoc_STRVAR(union_doc, |
| 911 | "Return the union of two sets as a new set.\n\ |
| 912 | \n\ |
| 913 | (i.e. all elements that are in either set.)"); |
| 914 | |
| 915 | static PyObject * |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 916 | set_or(PySetObject *so, PyObject *other) |
| 917 | { |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 918 | if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) { |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 919 | Py_INCREF(Py_NotImplemented); |
| 920 | return Py_NotImplemented; |
| 921 | } |
| 922 | return set_union(so, other); |
| 923 | } |
| 924 | |
| 925 | static PyObject * |
| 926 | set_ior(PySetObject *so, PyObject *other) |
| 927 | { |
| 928 | PyObject *result; |
| 929 | |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 930 | if (!PyAnySet_Check(other)) { |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 931 | Py_INCREF(Py_NotImplemented); |
| 932 | return Py_NotImplemented; |
| 933 | } |
Raymond Hettinger | a38123e | 2003-11-24 22:18:49 +0000 | [diff] [blame] | 934 | result = set_update(so, other); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 935 | if (result == NULL) |
| 936 | return NULL; |
| 937 | Py_DECREF(result); |
| 938 | Py_INCREF(so); |
| 939 | return (PyObject *)so; |
| 940 | } |
| 941 | |
| 942 | static PyObject * |
| 943 | set_intersection(PySetObject *so, PyObject *other) |
| 944 | { |
| 945 | PySetObject *result; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 946 | PyObject *item, *it, *tmp; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 947 | |
| 948 | result = (PySetObject *)make_new_set(so->ob_type, NULL); |
| 949 | if (result == NULL) |
| 950 | return NULL; |
Raymond Hettinger | f5f41bf | 2003-11-24 02:57:33 +0000 | [diff] [blame] | 951 | |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 952 | if (PyAnySet_Check(other) && set_len(other) > set_len((PyObject *)so)) { |
| 953 | tmp = (PyObject *)so; |
| 954 | so = (PySetObject *)other; |
Raymond Hettinger | a3b11e7 | 2003-12-31 14:08:58 +0000 | [diff] [blame] | 955 | other = tmp; |
| 956 | } |
| 957 | |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 958 | if (PyAnySet_Check(other)) { |
Raymond Hettinger | a3b11e7 | 2003-12-31 14:08:58 +0000 | [diff] [blame] | 959 | int pos = 0; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 960 | while (set_next_internal((PySetObject *)other, &pos, &item)) { |
| 961 | if (set_contains_internal(so, item)) { |
| 962 | if (set_add_internal(result, item) == -1) { |
Raymond Hettinger | a3b11e7 | 2003-12-31 14:08:58 +0000 | [diff] [blame] | 963 | Py_DECREF(result); |
| 964 | return NULL; |
| 965 | } |
| 966 | } |
| 967 | } |
| 968 | return (PyObject *)result; |
Raymond Hettinger | f5f41bf | 2003-11-24 02:57:33 +0000 | [diff] [blame] | 969 | } |
| 970 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 971 | it = PyObject_GetIter(other); |
| 972 | if (it == NULL) { |
| 973 | Py_DECREF(result); |
| 974 | return NULL; |
| 975 | } |
| 976 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 977 | while ((item = PyIter_Next(it)) != NULL) { |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 978 | if (set_contains_internal(so, item)) { |
| 979 | if (set_add_internal(result, item) == -1) { |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 980 | Py_DECREF(it); |
| 981 | Py_DECREF(result); |
| 982 | Py_DECREF(item); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 983 | return NULL; |
| 984 | } |
| 985 | } |
| 986 | Py_DECREF(item); |
| 987 | } |
| 988 | Py_DECREF(it); |
| 989 | if (PyErr_Occurred()) { |
| 990 | Py_DECREF(result); |
| 991 | return NULL; |
| 992 | } |
| 993 | return (PyObject *)result; |
| 994 | } |
| 995 | |
| 996 | PyDoc_STRVAR(intersection_doc, |
| 997 | "Return the intersection of two sets as a new set.\n\ |
| 998 | \n\ |
| 999 | (i.e. all elements that are in both sets.)"); |
| 1000 | |
| 1001 | static PyObject * |
| 1002 | set_intersection_update(PySetObject *so, PyObject *other) |
| 1003 | { |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1004 | PyObject *tmp; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1005 | |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1006 | tmp = set_intersection(so, other); |
| 1007 | if (tmp == NULL) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1008 | return NULL; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1009 | set_swap_bodies(so, (PySetObject *)tmp); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1010 | Py_DECREF(tmp); |
| 1011 | Py_RETURN_NONE; |
| 1012 | } |
| 1013 | |
| 1014 | PyDoc_STRVAR(intersection_update_doc, |
| 1015 | "Update a set with the intersection of itself and another."); |
| 1016 | |
| 1017 | static PyObject * |
| 1018 | set_and(PySetObject *so, PyObject *other) |
| 1019 | { |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 1020 | if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) { |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1021 | Py_INCREF(Py_NotImplemented); |
| 1022 | return Py_NotImplemented; |
| 1023 | } |
| 1024 | return set_intersection(so, other); |
| 1025 | } |
| 1026 | |
| 1027 | static PyObject * |
| 1028 | set_iand(PySetObject *so, PyObject *other) |
| 1029 | { |
| 1030 | PyObject *result; |
| 1031 | |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 1032 | if (!PyAnySet_Check(other)) { |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1033 | Py_INCREF(Py_NotImplemented); |
| 1034 | return Py_NotImplemented; |
| 1035 | } |
| 1036 | result = set_intersection_update(so, other); |
| 1037 | if (result == NULL) |
| 1038 | return NULL; |
| 1039 | Py_DECREF(result); |
| 1040 | Py_INCREF(so); |
| 1041 | return (PyObject *)so; |
| 1042 | } |
| 1043 | |
| 1044 | static PyObject * |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1045 | set_difference_update(PySetObject *so, PyObject *other) |
| 1046 | { |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1047 | PyObject *item, *it; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1048 | |
| 1049 | it = PyObject_GetIter(other); |
| 1050 | if (it == NULL) |
| 1051 | return NULL; |
| 1052 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1053 | while ((item = PyIter_Next(it)) != NULL) { |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1054 | if (set_discard_internal(so, item) == -1) { |
| 1055 | Py_DECREF(it); |
| 1056 | Py_DECREF(item); |
| 1057 | return NULL; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1058 | } |
| 1059 | Py_DECREF(item); |
| 1060 | } |
| 1061 | Py_DECREF(it); |
| 1062 | if (PyErr_Occurred()) |
| 1063 | return NULL; |
| 1064 | Py_RETURN_NONE; |
| 1065 | } |
| 1066 | |
| 1067 | PyDoc_STRVAR(difference_update_doc, |
| 1068 | "Remove all elements of another set from this set."); |
| 1069 | |
| 1070 | static PyObject * |
Raymond Hettinger | fb4e33a | 2003-12-15 13:23:55 +0000 | [diff] [blame] | 1071 | set_difference(PySetObject *so, PyObject *other) |
| 1072 | { |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1073 | PyObject *tmp, *key, *result; |
Raymond Hettinger | fb4e33a | 2003-12-15 13:23:55 +0000 | [diff] [blame] | 1074 | int pos = 0; |
| 1075 | |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1076 | if (!PyAnySet_Check(other) && !PyDict_Check(other)) { |
Raymond Hettinger | fb4e33a | 2003-12-15 13:23:55 +0000 | [diff] [blame] | 1077 | result = set_copy(so); |
| 1078 | if (result == NULL) |
| 1079 | return result; |
| 1080 | tmp = set_difference_update((PySetObject *)result, other); |
| 1081 | if (tmp != NULL) { |
| 1082 | Py_DECREF(tmp); |
| 1083 | return result; |
| 1084 | } |
| 1085 | Py_DECREF(result); |
| 1086 | return NULL; |
| 1087 | } |
| 1088 | |
| 1089 | result = make_new_set(so->ob_type, NULL); |
| 1090 | if (result == NULL) |
| 1091 | return NULL; |
Raymond Hettinger | fb4e33a | 2003-12-15 13:23:55 +0000 | [diff] [blame] | 1092 | |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1093 | if (PyDict_Check(other)) { |
| 1094 | while (set_next_internal(so, &pos, &key)) { |
| 1095 | if (!PyDict_Contains(other, key)) { |
| 1096 | if (set_add_internal((PySetObject *)result, key) == -1) |
| 1097 | return NULL; |
| 1098 | } |
| 1099 | } |
| 1100 | return result; |
| 1101 | } |
| 1102 | |
| 1103 | while (set_next_internal(so, &pos, &key)) { |
| 1104 | if (!set_contains_internal((PySetObject *)other, key)) { |
| 1105 | if (set_add_internal((PySetObject *)result, key) == -1) |
Raymond Hettinger | fb4e33a | 2003-12-15 13:23:55 +0000 | [diff] [blame] | 1106 | return NULL; |
| 1107 | } |
| 1108 | } |
| 1109 | return result; |
| 1110 | } |
| 1111 | |
| 1112 | PyDoc_STRVAR(difference_doc, |
| 1113 | "Return the difference of two sets as a new set.\n\ |
| 1114 | \n\ |
| 1115 | (i.e. all elements that are in this set but not the other.)"); |
| 1116 | static PyObject * |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1117 | set_sub(PySetObject *so, PyObject *other) |
| 1118 | { |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 1119 | if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) { |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1120 | Py_INCREF(Py_NotImplemented); |
| 1121 | return Py_NotImplemented; |
| 1122 | } |
| 1123 | return set_difference(so, other); |
| 1124 | } |
| 1125 | |
| 1126 | static PyObject * |
| 1127 | set_isub(PySetObject *so, PyObject *other) |
| 1128 | { |
| 1129 | PyObject *result; |
| 1130 | |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 1131 | if (!PyAnySet_Check(other)) { |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1132 | Py_INCREF(Py_NotImplemented); |
| 1133 | return Py_NotImplemented; |
| 1134 | } |
| 1135 | result = set_difference_update(so, other); |
| 1136 | if (result == NULL) |
| 1137 | return NULL; |
| 1138 | Py_DECREF(result); |
| 1139 | Py_INCREF(so); |
| 1140 | return (PyObject *)so; |
| 1141 | } |
| 1142 | |
| 1143 | static PyObject * |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1144 | set_symmetric_difference_update(PySetObject *so, PyObject *other) |
| 1145 | { |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1146 | PySetObject *otherset; |
| 1147 | PyObject *key; |
Raymond Hettinger | dc5ae11 | 2003-12-13 14:46:46 +0000 | [diff] [blame] | 1148 | int pos = 0; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1149 | |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1150 | if (PyDict_Check(other)) { |
| 1151 | PyObject *value; |
| 1152 | int rv; |
| 1153 | while (PyDict_Next(other, &pos, &key, &value)) { |
| 1154 | rv = set_discard_internal(so, key); |
| 1155 | if (rv == -1) |
| 1156 | return NULL; |
| 1157 | if (rv == DISCARD_NOTFOUND) { |
| 1158 | if (set_add_internal(so, key) == -1) |
| 1159 | return NULL; |
| 1160 | } |
| 1161 | } |
| 1162 | Py_RETURN_NONE; |
| 1163 | } |
| 1164 | |
| 1165 | if (PyAnySet_Check(other)) { |
| 1166 | Py_INCREF(other); |
| 1167 | otherset = (PySetObject *)other; |
| 1168 | } else { |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1169 | otherset = (PySetObject *)make_new_set(so->ob_type, other); |
| 1170 | if (otherset == NULL) |
| 1171 | return NULL; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1172 | } |
| 1173 | |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1174 | while (set_next_internal(otherset, &pos, &key)) { |
| 1175 | int rv = set_discard_internal(so, key); |
| 1176 | if (rv == -1) { |
| 1177 | Py_XDECREF(otherset); |
| 1178 | return NULL; |
| 1179 | } |
| 1180 | if (rv == DISCARD_NOTFOUND) { |
| 1181 | if (set_add_internal(so, key) == -1) { |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1182 | Py_XDECREF(otherset); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1183 | return NULL; |
| 1184 | } |
| 1185 | } |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1186 | } |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1187 | Py_DECREF(otherset); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1188 | Py_RETURN_NONE; |
| 1189 | } |
| 1190 | |
| 1191 | PyDoc_STRVAR(symmetric_difference_update_doc, |
| 1192 | "Update a set with the symmetric difference of itself and another."); |
| 1193 | |
| 1194 | static PyObject * |
Raymond Hettinger | f5f41bf | 2003-11-24 02:57:33 +0000 | [diff] [blame] | 1195 | set_symmetric_difference(PySetObject *so, PyObject *other) |
| 1196 | { |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1197 | PyObject *rv; |
| 1198 | PySetObject *otherset; |
Raymond Hettinger | f5f41bf | 2003-11-24 02:57:33 +0000 | [diff] [blame] | 1199 | |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1200 | otherset = (PySetObject *)make_new_set(so->ob_type, other); |
| 1201 | if (otherset == NULL) |
Raymond Hettinger | f5f41bf | 2003-11-24 02:57:33 +0000 | [diff] [blame] | 1202 | return NULL; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1203 | rv = set_symmetric_difference_update(otherset, (PyObject *)so); |
| 1204 | if (rv == NULL) |
| 1205 | return NULL; |
| 1206 | Py_DECREF(rv); |
| 1207 | return (PyObject *)otherset; |
Raymond Hettinger | f5f41bf | 2003-11-24 02:57:33 +0000 | [diff] [blame] | 1208 | } |
| 1209 | |
| 1210 | PyDoc_STRVAR(symmetric_difference_doc, |
| 1211 | "Return the symmetric difference of two sets as a new set.\n\ |
| 1212 | \n\ |
| 1213 | (i.e. all elements that are in exactly one of the sets.)"); |
| 1214 | |
| 1215 | static PyObject * |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1216 | set_xor(PySetObject *so, PyObject *other) |
| 1217 | { |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 1218 | if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) { |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1219 | Py_INCREF(Py_NotImplemented); |
| 1220 | return Py_NotImplemented; |
| 1221 | } |
| 1222 | return set_symmetric_difference(so, other); |
| 1223 | } |
| 1224 | |
| 1225 | static PyObject * |
| 1226 | set_ixor(PySetObject *so, PyObject *other) |
| 1227 | { |
| 1228 | PyObject *result; |
| 1229 | |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 1230 | if (!PyAnySet_Check(other)) { |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1231 | Py_INCREF(Py_NotImplemented); |
| 1232 | return Py_NotImplemented; |
| 1233 | } |
| 1234 | result = set_symmetric_difference_update(so, other); |
| 1235 | if (result == NULL) |
| 1236 | return NULL; |
| 1237 | Py_DECREF(result); |
| 1238 | Py_INCREF(so); |
| 1239 | return (PyObject *)so; |
| 1240 | } |
| 1241 | |
| 1242 | static PyObject * |
| 1243 | set_issubset(PySetObject *so, PyObject *other) |
| 1244 | { |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1245 | PyObject *tmp, *result; |
| 1246 | PyObject *key; |
Raymond Hettinger | dc5ae11 | 2003-12-13 14:46:46 +0000 | [diff] [blame] | 1247 | int pos = 0; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1248 | |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 1249 | if (!PyAnySet_Check(other)) { |
Raymond Hettinger | 3fbec70 | 2003-11-21 07:56:36 +0000 | [diff] [blame] | 1250 | tmp = make_new_set(&PySet_Type, other); |
| 1251 | if (tmp == NULL) |
| 1252 | return NULL; |
| 1253 | result = set_issubset(so, tmp); |
| 1254 | Py_DECREF(tmp); |
| 1255 | return result; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1256 | } |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1257 | if (set_len((PyObject *)so) > set_len(other)) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1258 | Py_RETURN_FALSE; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1259 | |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1260 | while (set_next_internal(so, &pos, &key)) { |
| 1261 | if (!set_contains_internal((PySetObject *)other, key)) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1262 | Py_RETURN_FALSE; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1263 | } |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1264 | Py_RETURN_TRUE; |
| 1265 | } |
| 1266 | |
| 1267 | PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set."); |
| 1268 | |
| 1269 | static PyObject * |
| 1270 | set_issuperset(PySetObject *so, PyObject *other) |
| 1271 | { |
Raymond Hettinger | 3fbec70 | 2003-11-21 07:56:36 +0000 | [diff] [blame] | 1272 | PyObject *tmp, *result; |
| 1273 | |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 1274 | if (!PyAnySet_Check(other)) { |
Raymond Hettinger | 3fbec70 | 2003-11-21 07:56:36 +0000 | [diff] [blame] | 1275 | tmp = make_new_set(&PySet_Type, other); |
| 1276 | if (tmp == NULL) |
| 1277 | return NULL; |
| 1278 | result = set_issuperset(so, tmp); |
| 1279 | Py_DECREF(tmp); |
| 1280 | return result; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1281 | } |
| 1282 | return set_issubset((PySetObject *)other, (PyObject *)so); |
| 1283 | } |
| 1284 | |
| 1285 | PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set."); |
| 1286 | |
| 1287 | static long |
| 1288 | set_nohash(PyObject *self) |
| 1289 | { |
| 1290 | PyErr_SetString(PyExc_TypeError, "set objects are unhashable"); |
| 1291 | return -1; |
| 1292 | } |
| 1293 | |
| 1294 | static int |
| 1295 | set_nocmp(PyObject *self) |
| 1296 | { |
| 1297 | PyErr_SetString(PyExc_TypeError, "cannot compare sets using cmp()"); |
| 1298 | return -1; |
| 1299 | } |
| 1300 | |
| 1301 | static long |
| 1302 | frozenset_hash(PyObject *self) |
| 1303 | { |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1304 | PyObject *key; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1305 | PySetObject *so = (PySetObject *)self; |
Raymond Hettinger | dc5ae11 | 2003-12-13 14:46:46 +0000 | [diff] [blame] | 1306 | int pos = 0; |
Raymond Hettinger | c978633 | 2004-06-10 22:41:48 +0000 | [diff] [blame] | 1307 | long hash = 1927868237L; |
Raymond Hettinger | 438e02d | 2003-12-13 19:38:47 +0000 | [diff] [blame] | 1308 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1309 | if (so->hash != -1) |
| 1310 | return so->hash; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1311 | |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1312 | hash *= set_len(self) + 1; |
| 1313 | while (set_next_internal(so, &pos, &key)) { |
Raymond Hettinger | c978633 | 2004-06-10 22:41:48 +0000 | [diff] [blame] | 1314 | /* Work to increase the bit dispersion for closely spaced hash |
| 1315 | values. The is important because some use cases have many |
| 1316 | combinations of a small number of elements with nearby |
| 1317 | hashes so that many distinct combinations collapse to only |
| 1318 | a handful of distinct hash values. */ |
| 1319 | long h = PyObject_Hash(key); |
| 1320 | hash ^= (h ^ (h << 16) ^ 89869747L) * 3644798167u; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1321 | } |
Raymond Hettinger | c978633 | 2004-06-10 22:41:48 +0000 | [diff] [blame] | 1322 | hash = hash * 69069L + 907133923L; |
Raymond Hettinger | 27e403e | 2004-06-10 21:38:41 +0000 | [diff] [blame] | 1323 | if (hash == -1) |
| 1324 | hash = 590923713L; |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 1325 | so->hash = hash; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1326 | return hash; |
| 1327 | } |
| 1328 | |
| 1329 | static PyObject * |
| 1330 | set_richcompare(PySetObject *v, PyObject *w, int op) |
| 1331 | { |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1332 | PyObject *r1, *r2; |
| 1333 | |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 1334 | if(!PyAnySet_Check(w)) { |
| 1335 | if (op == Py_EQ) |
| 1336 | Py_RETURN_FALSE; |
| 1337 | if (op == Py_NE) |
| 1338 | Py_RETURN_TRUE; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1339 | PyErr_SetString(PyExc_TypeError, "can only compare to a set"); |
| 1340 | return NULL; |
| 1341 | } |
| 1342 | switch (op) { |
| 1343 | case Py_EQ: |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1344 | if (set_len((PyObject *)v) != set_len(w)) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1345 | Py_RETURN_FALSE; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1346 | return set_issubset(v, w); |
| 1347 | case Py_NE: |
| 1348 | if (set_len((PyObject *)v) != set_len(w)) |
| 1349 | Py_RETURN_TRUE; |
| 1350 | r1 = set_issubset(v, w); |
| 1351 | assert (r1 != NULL); |
| 1352 | r2 = PyBool_FromLong(PyObject_Not(r1)); |
| 1353 | Py_DECREF(r1); |
| 1354 | return r2; |
| 1355 | case Py_LE: |
| 1356 | return set_issubset(v, w); |
| 1357 | case Py_GE: |
| 1358 | return set_issuperset(v, w); |
| 1359 | case Py_LT: |
| 1360 | if (set_len((PyObject *)v) >= set_len(w)) |
| 1361 | Py_RETURN_FALSE; |
| 1362 | return set_issubset(v, w); |
| 1363 | case Py_GT: |
| 1364 | if (set_len((PyObject *)v) <= set_len(w)) |
| 1365 | Py_RETURN_FALSE; |
| 1366 | return set_issuperset(v, w); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1367 | } |
| 1368 | Py_INCREF(Py_NotImplemented); |
| 1369 | return Py_NotImplemented; |
| 1370 | } |
| 1371 | |
| 1372 | static PyObject * |
| 1373 | set_repr(PySetObject *so) |
| 1374 | { |
| 1375 | PyObject *keys, *result, *listrepr; |
| 1376 | |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1377 | keys = PySequence_List((PyObject *)so); |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 1378 | if (keys == NULL) |
| 1379 | return NULL; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1380 | listrepr = PyObject_Repr(keys); |
| 1381 | Py_DECREF(keys); |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 1382 | if (listrepr == NULL) |
| 1383 | return NULL; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1384 | |
| 1385 | result = PyString_FromFormat("%s(%s)", so->ob_type->tp_name, |
| 1386 | PyString_AS_STRING(listrepr)); |
| 1387 | Py_DECREF(listrepr); |
| 1388 | return result; |
| 1389 | } |
| 1390 | |
| 1391 | static int |
| 1392 | set_tp_print(PySetObject *so, FILE *fp, int flags) |
| 1393 | { |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1394 | PyObject *key; |
Raymond Hettinger | a3b11e7 | 2003-12-31 14:08:58 +0000 | [diff] [blame] | 1395 | int pos=0; |
| 1396 | char *emit = ""; /* No separator emitted on first pass */ |
| 1397 | char *separator = ", "; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1398 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1399 | fprintf(fp, "%s([", so->ob_type->tp_name); |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1400 | while (set_next_internal(so, &pos, &key)) { |
Raymond Hettinger | a3b11e7 | 2003-12-31 14:08:58 +0000 | [diff] [blame] | 1401 | fputs(emit, fp); |
| 1402 | emit = separator; |
Raymond Hettinger | dc5ae11 | 2003-12-13 14:46:46 +0000 | [diff] [blame] | 1403 | if (PyObject_Print(key, fp, 0) != 0) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1404 | return -1; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1405 | } |
Raymond Hettinger | a3b11e7 | 2003-12-31 14:08:58 +0000 | [diff] [blame] | 1406 | fputs("])", fp); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1407 | return 0; |
| 1408 | } |
| 1409 | |
| 1410 | static PyObject * |
| 1411 | set_clear(PySetObject *so) |
| 1412 | { |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1413 | set_clear_internal(so); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1414 | so->hash = -1; |
Raymond Hettinger | 438e02d | 2003-12-13 19:38:47 +0000 | [diff] [blame] | 1415 | Py_RETURN_NONE; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1416 | } |
| 1417 | |
| 1418 | PyDoc_STRVAR(clear_doc, "Remove all elements from this set."); |
| 1419 | |
Raymond Hettinger | bb999b5 | 2005-06-18 21:00:26 +0000 | [diff] [blame] | 1420 | static int |
| 1421 | set_tp_clear(PySetObject *so) |
| 1422 | { |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1423 | set_clear_internal(so); |
Raymond Hettinger | bb999b5 | 2005-06-18 21:00:26 +0000 | [diff] [blame] | 1424 | so->hash = -1; |
| 1425 | return 0; |
| 1426 | } |
| 1427 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1428 | static PyObject * |
| 1429 | set_add(PySetObject *so, PyObject *item) |
| 1430 | { |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1431 | if (set_add_internal(so, item) == -1) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1432 | return NULL; |
Raymond Hettinger | 438e02d | 2003-12-13 19:38:47 +0000 | [diff] [blame] | 1433 | Py_RETURN_NONE; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1434 | } |
| 1435 | |
| 1436 | PyDoc_STRVAR(add_doc, |
| 1437 | "Add an element to a set.\n\ |
| 1438 | \n\ |
| 1439 | This has no effect if the element is already present."); |
| 1440 | |
| 1441 | static PyObject * |
| 1442 | set_remove(PySetObject *so, PyObject *item) |
| 1443 | { |
Raymond Hettinger | 0deab62 | 2003-12-13 18:53:18 +0000 | [diff] [blame] | 1444 | PyObject *tmp, *result; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1445 | int rv; |
Raymond Hettinger | bfd334a | 2003-11-22 03:55:23 +0000 | [diff] [blame] | 1446 | |
Raymond Hettinger | 0deab62 | 2003-12-13 18:53:18 +0000 | [diff] [blame] | 1447 | if (PyType_IsSubtype(item->ob_type, &PySet_Type)) { |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1448 | tmp = make_new_set(&PyFrozenSet_Type, NULL); |
Raymond Hettinger | bfd334a | 2003-11-22 03:55:23 +0000 | [diff] [blame] | 1449 | if (tmp == NULL) |
| 1450 | return NULL; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1451 | set_swap_bodies((PySetObject *)item, (PySetObject *)tmp); |
Raymond Hettinger | 0deab62 | 2003-12-13 18:53:18 +0000 | [diff] [blame] | 1452 | result = set_remove(so, tmp); |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1453 | set_swap_bodies((PySetObject *)item, (PySetObject *)tmp); |
Raymond Hettinger | bfd334a | 2003-11-22 03:55:23 +0000 | [diff] [blame] | 1454 | Py_DECREF(tmp); |
Raymond Hettinger | 0deab62 | 2003-12-13 18:53:18 +0000 | [diff] [blame] | 1455 | return result; |
Raymond Hettinger | bfd334a | 2003-11-22 03:55:23 +0000 | [diff] [blame] | 1456 | } |
Raymond Hettinger | 0deab62 | 2003-12-13 18:53:18 +0000 | [diff] [blame] | 1457 | |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1458 | rv = set_discard_internal(so, item); |
| 1459 | if (rv == -1) |
Raymond Hettinger | 0deab62 | 2003-12-13 18:53:18 +0000 | [diff] [blame] | 1460 | return NULL; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1461 | else if (rv == DISCARD_NOTFOUND) { |
| 1462 | PyErr_SetObject(PyExc_KeyError, item); |
| 1463 | return NULL; |
| 1464 | } |
Raymond Hettinger | 438e02d | 2003-12-13 19:38:47 +0000 | [diff] [blame] | 1465 | Py_RETURN_NONE; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1466 | } |
| 1467 | |
| 1468 | PyDoc_STRVAR(remove_doc, |
| 1469 | "Remove an element from a set; it must be a member.\n\ |
| 1470 | \n\ |
| 1471 | If the element is not a member, raise a KeyError."); |
| 1472 | |
| 1473 | static PyObject * |
| 1474 | set_discard(PySetObject *so, PyObject *item) |
| 1475 | { |
Raymond Hettinger | 0deab62 | 2003-12-13 18:53:18 +0000 | [diff] [blame] | 1476 | PyObject *tmp, *result; |
| 1477 | |
| 1478 | if (PyType_IsSubtype(item->ob_type, &PySet_Type)) { |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1479 | tmp = make_new_set(&PyFrozenSet_Type, NULL); |
Raymond Hettinger | 0deab62 | 2003-12-13 18:53:18 +0000 | [diff] [blame] | 1480 | if (tmp == NULL) |
| 1481 | return NULL; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1482 | set_swap_bodies((PySetObject *)item, (PySetObject *)tmp); |
Raymond Hettinger | 0deab62 | 2003-12-13 18:53:18 +0000 | [diff] [blame] | 1483 | result = set_discard(so, tmp); |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1484 | set_swap_bodies((PySetObject *)item, (PySetObject *)tmp); |
Raymond Hettinger | 0deab62 | 2003-12-13 18:53:18 +0000 | [diff] [blame] | 1485 | Py_DECREF(tmp); |
| 1486 | return result; |
| 1487 | } |
Raymond Hettinger | bfd334a | 2003-11-22 03:55:23 +0000 | [diff] [blame] | 1488 | |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1489 | if (set_discard_internal(so, item) == -1) |
| 1490 | return NULL; |
Raymond Hettinger | 438e02d | 2003-12-13 19:38:47 +0000 | [diff] [blame] | 1491 | Py_RETURN_NONE; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1492 | } |
| 1493 | |
| 1494 | PyDoc_STRVAR(discard_doc, |
| 1495 | "Remove an element from a set if it is a member.\n\ |
| 1496 | \n\ |
| 1497 | If the element is not a member, do nothing."); |
| 1498 | |
| 1499 | static PyObject * |
| 1500 | set_pop(PySetObject *so) |
| 1501 | { |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1502 | PyObject *key; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1503 | int pos = 0; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1504 | int rv; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1505 | |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1506 | if (!set_next_internal(so, &pos, &key)) { |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1507 | PyErr_SetString(PyExc_KeyError, "pop from an empty set"); |
| 1508 | return NULL; |
| 1509 | } |
| 1510 | Py_INCREF(key); |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1511 | |
| 1512 | rv = set_discard_internal(so, key); |
| 1513 | if (rv == -1) { |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 1514 | Py_DECREF(key); |
| 1515 | return NULL; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1516 | } else if (rv == DISCARD_NOTFOUND) { |
| 1517 | Py_DECREF(key); |
| 1518 | PyErr_SetObject(PyExc_KeyError, key); |
| 1519 | return NULL; |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 1520 | } |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1521 | return key; |
| 1522 | } |
| 1523 | |
| 1524 | PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element."); |
| 1525 | |
| 1526 | static PyObject * |
| 1527 | set_reduce(PySetObject *so) |
| 1528 | { |
Raymond Hettinger | 15056a5 | 2004-11-09 07:25:31 +0000 | [diff] [blame] | 1529 | PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1530 | |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1531 | keys = PySequence_List((PyObject *)so); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1532 | if (keys == NULL) |
| 1533 | goto done; |
| 1534 | args = PyTuple_Pack(1, keys); |
| 1535 | if (args == NULL) |
| 1536 | goto done; |
Raymond Hettinger | 15056a5 | 2004-11-09 07:25:31 +0000 | [diff] [blame] | 1537 | dict = PyObject_GetAttrString((PyObject *)so, "__dict__"); |
| 1538 | if (dict == NULL) { |
| 1539 | PyErr_Clear(); |
| 1540 | dict = Py_None; |
| 1541 | Py_INCREF(dict); |
| 1542 | } |
| 1543 | result = PyTuple_Pack(3, so->ob_type, args, dict); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1544 | done: |
| 1545 | Py_XDECREF(args); |
| 1546 | Py_XDECREF(keys); |
Raymond Hettinger | 15056a5 | 2004-11-09 07:25:31 +0000 | [diff] [blame] | 1547 | Py_XDECREF(dict); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1548 | return result; |
| 1549 | } |
| 1550 | |
| 1551 | PyDoc_STRVAR(reduce_doc, "Return state information for pickling."); |
| 1552 | |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 1553 | static int |
| 1554 | set_init(PySetObject *self, PyObject *args, PyObject *kwds) |
| 1555 | { |
| 1556 | PyObject *iterable = NULL; |
| 1557 | PyObject *result; |
| 1558 | |
| 1559 | if (!PyAnySet_Check(self)) |
| 1560 | return -1; |
| 1561 | if (!PyArg_UnpackTuple(args, self->ob_type->tp_name, 0, 1, &iterable)) |
| 1562 | return -1; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1563 | set_clear_internal(self); |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 1564 | self->hash = -1; |
| 1565 | if (iterable == NULL) |
| 1566 | return 0; |
Raymond Hettinger | a38123e | 2003-11-24 22:18:49 +0000 | [diff] [blame] | 1567 | result = set_update(self, iterable); |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 1568 | if (result != NULL) { |
| 1569 | Py_DECREF(result); |
| 1570 | return 0; |
| 1571 | } |
| 1572 | return -1; |
| 1573 | } |
| 1574 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1575 | static PySequenceMethods set_as_sequence = { |
| 1576 | (inquiry)set_len, /* sq_length */ |
| 1577 | 0, /* sq_concat */ |
| 1578 | 0, /* sq_repeat */ |
| 1579 | 0, /* sq_item */ |
| 1580 | 0, /* sq_slice */ |
| 1581 | 0, /* sq_ass_item */ |
| 1582 | 0, /* sq_ass_slice */ |
| 1583 | (objobjproc)set_contains, /* sq_contains */ |
| 1584 | }; |
| 1585 | |
| 1586 | /* set object ********************************************************/ |
| 1587 | |
| 1588 | static PyMethodDef set_methods[] = { |
| 1589 | {"add", (PyCFunction)set_add, METH_O, |
| 1590 | add_doc}, |
| 1591 | {"clear", (PyCFunction)set_clear, METH_NOARGS, |
| 1592 | clear_doc}, |
Raymond Hettinger | 0deab62 | 2003-12-13 18:53:18 +0000 | [diff] [blame] | 1593 | {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST, |
Raymond Hettinger | 8f5cdaa | 2003-12-13 11:26:12 +0000 | [diff] [blame] | 1594 | contains_doc}, |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1595 | {"copy", (PyCFunction)set_copy, METH_NOARGS, |
| 1596 | copy_doc}, |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1597 | {"discard", (PyCFunction)set_discard, METH_O, |
| 1598 | discard_doc}, |
| 1599 | {"difference", (PyCFunction)set_difference, METH_O, |
| 1600 | difference_doc}, |
| 1601 | {"difference_update", (PyCFunction)set_difference_update, METH_O, |
| 1602 | difference_update_doc}, |
| 1603 | {"intersection",(PyCFunction)set_intersection, METH_O, |
| 1604 | intersection_doc}, |
| 1605 | {"intersection_update",(PyCFunction)set_intersection_update, METH_O, |
| 1606 | intersection_update_doc}, |
| 1607 | {"issubset", (PyCFunction)set_issubset, METH_O, |
| 1608 | issubset_doc}, |
| 1609 | {"issuperset", (PyCFunction)set_issuperset, METH_O, |
| 1610 | issuperset_doc}, |
| 1611 | {"pop", (PyCFunction)set_pop, METH_NOARGS, |
| 1612 | pop_doc}, |
| 1613 | {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS, |
| 1614 | reduce_doc}, |
| 1615 | {"remove", (PyCFunction)set_remove, METH_O, |
| 1616 | remove_doc}, |
| 1617 | {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O, |
| 1618 | symmetric_difference_doc}, |
| 1619 | {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O, |
| 1620 | symmetric_difference_update_doc}, |
| 1621 | {"union", (PyCFunction)set_union, METH_O, |
| 1622 | union_doc}, |
Raymond Hettinger | a38123e | 2003-11-24 22:18:49 +0000 | [diff] [blame] | 1623 | {"update", (PyCFunction)set_update, METH_O, |
| 1624 | update_doc}, |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1625 | {NULL, NULL} /* sentinel */ |
| 1626 | }; |
| 1627 | |
| 1628 | static PyNumberMethods set_as_number = { |
| 1629 | 0, /*nb_add*/ |
| 1630 | (binaryfunc)set_sub, /*nb_subtract*/ |
| 1631 | 0, /*nb_multiply*/ |
| 1632 | 0, /*nb_divide*/ |
| 1633 | 0, /*nb_remainder*/ |
| 1634 | 0, /*nb_divmod*/ |
| 1635 | 0, /*nb_power*/ |
| 1636 | 0, /*nb_negative*/ |
| 1637 | 0, /*nb_positive*/ |
| 1638 | 0, /*nb_absolute*/ |
| 1639 | 0, /*nb_nonzero*/ |
| 1640 | 0, /*nb_invert*/ |
| 1641 | 0, /*nb_lshift*/ |
| 1642 | 0, /*nb_rshift*/ |
| 1643 | (binaryfunc)set_and, /*nb_and*/ |
| 1644 | (binaryfunc)set_xor, /*nb_xor*/ |
| 1645 | (binaryfunc)set_or, /*nb_or*/ |
| 1646 | 0, /*nb_coerce*/ |
| 1647 | 0, /*nb_int*/ |
| 1648 | 0, /*nb_long*/ |
| 1649 | 0, /*nb_float*/ |
| 1650 | 0, /*nb_oct*/ |
| 1651 | 0, /*nb_hex*/ |
| 1652 | 0, /*nb_inplace_add*/ |
| 1653 | (binaryfunc)set_isub, /*nb_inplace_subtract*/ |
| 1654 | 0, /*nb_inplace_multiply*/ |
| 1655 | 0, /*nb_inplace_divide*/ |
| 1656 | 0, /*nb_inplace_remainder*/ |
| 1657 | 0, /*nb_inplace_power*/ |
| 1658 | 0, /*nb_inplace_lshift*/ |
| 1659 | 0, /*nb_inplace_rshift*/ |
| 1660 | (binaryfunc)set_iand, /*nb_inplace_and*/ |
| 1661 | (binaryfunc)set_ixor, /*nb_inplace_xor*/ |
| 1662 | (binaryfunc)set_ior, /*nb_inplace_or*/ |
| 1663 | }; |
| 1664 | |
| 1665 | PyDoc_STRVAR(set_doc, |
| 1666 | "set(iterable) --> set object\n\ |
| 1667 | \n\ |
| 1668 | Build an unordered collection."); |
| 1669 | |
| 1670 | PyTypeObject PySet_Type = { |
| 1671 | PyObject_HEAD_INIT(&PyType_Type) |
| 1672 | 0, /* ob_size */ |
| 1673 | "set", /* tp_name */ |
| 1674 | sizeof(PySetObject), /* tp_basicsize */ |
| 1675 | 0, /* tp_itemsize */ |
| 1676 | /* methods */ |
| 1677 | (destructor)set_dealloc, /* tp_dealloc */ |
| 1678 | (printfunc)set_tp_print, /* tp_print */ |
| 1679 | 0, /* tp_getattr */ |
| 1680 | 0, /* tp_setattr */ |
| 1681 | (cmpfunc)set_nocmp, /* tp_compare */ |
| 1682 | (reprfunc)set_repr, /* tp_repr */ |
| 1683 | &set_as_number, /* tp_as_number */ |
| 1684 | &set_as_sequence, /* tp_as_sequence */ |
| 1685 | 0, /* tp_as_mapping */ |
| 1686 | set_nohash, /* tp_hash */ |
| 1687 | 0, /* tp_call */ |
| 1688 | 0, /* tp_str */ |
| 1689 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 1690 | 0, /* tp_setattro */ |
| 1691 | 0, /* tp_as_buffer */ |
Raymond Hettinger | bb999b5 | 2005-06-18 21:00:26 +0000 | [diff] [blame] | 1692 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES | |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1693 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1694 | set_doc, /* tp_doc */ |
Raymond Hettinger | bb999b5 | 2005-06-18 21:00:26 +0000 | [diff] [blame] | 1695 | (traverseproc)set_traverse, /* tp_traverse */ |
| 1696 | (inquiry)set_tp_clear, /* tp_clear */ |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1697 | (richcmpfunc)set_richcompare, /* tp_richcompare */ |
Raymond Hettinger | 691d805 | 2004-05-30 07:26:47 +0000 | [diff] [blame] | 1698 | offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */ |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1699 | (getiterfunc)set_iter, /* tp_iter */ |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1700 | 0, /* tp_iternext */ |
| 1701 | set_methods, /* tp_methods */ |
| 1702 | 0, /* tp_members */ |
| 1703 | 0, /* tp_getset */ |
| 1704 | 0, /* tp_base */ |
| 1705 | 0, /* tp_dict */ |
| 1706 | 0, /* tp_descr_get */ |
| 1707 | 0, /* tp_descr_set */ |
| 1708 | 0, /* tp_dictoffset */ |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 1709 | (initproc)set_init, /* tp_init */ |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1710 | PyType_GenericAlloc, /* tp_alloc */ |
| 1711 | set_new, /* tp_new */ |
Raymond Hettinger | bb999b5 | 2005-06-18 21:00:26 +0000 | [diff] [blame] | 1712 | PyObject_GC_Del, /* tp_free */ |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1713 | }; |
| 1714 | |
| 1715 | /* frozenset object ********************************************************/ |
| 1716 | |
| 1717 | |
| 1718 | static PyMethodDef frozenset_methods[] = { |
Raymond Hettinger | 0deab62 | 2003-12-13 18:53:18 +0000 | [diff] [blame] | 1719 | {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST, |
Raymond Hettinger | 8f5cdaa | 2003-12-13 11:26:12 +0000 | [diff] [blame] | 1720 | contains_doc}, |
Raymond Hettinger | 49ba4c3 | 2003-11-23 02:49:05 +0000 | [diff] [blame] | 1721 | {"copy", (PyCFunction)frozenset_copy, METH_NOARGS, |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1722 | copy_doc}, |
Raymond Hettinger | 49ba4c3 | 2003-11-23 02:49:05 +0000 | [diff] [blame] | 1723 | {"difference", (PyCFunction)set_difference, METH_O, |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1724 | difference_doc}, |
| 1725 | {"intersection",(PyCFunction)set_intersection, METH_O, |
| 1726 | intersection_doc}, |
Raymond Hettinger | 49ba4c3 | 2003-11-23 02:49:05 +0000 | [diff] [blame] | 1727 | {"issubset", (PyCFunction)set_issubset, METH_O, |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1728 | issubset_doc}, |
Raymond Hettinger | 49ba4c3 | 2003-11-23 02:49:05 +0000 | [diff] [blame] | 1729 | {"issuperset", (PyCFunction)set_issuperset, METH_O, |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1730 | issuperset_doc}, |
| 1731 | {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS, |
| 1732 | reduce_doc}, |
| 1733 | {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O, |
| 1734 | symmetric_difference_doc}, |
| 1735 | {"union", (PyCFunction)set_union, METH_O, |
| 1736 | union_doc}, |
| 1737 | {NULL, NULL} /* sentinel */ |
| 1738 | }; |
| 1739 | |
| 1740 | static PyNumberMethods frozenset_as_number = { |
| 1741 | 0, /*nb_add*/ |
| 1742 | (binaryfunc)set_sub, /*nb_subtract*/ |
| 1743 | 0, /*nb_multiply*/ |
| 1744 | 0, /*nb_divide*/ |
| 1745 | 0, /*nb_remainder*/ |
| 1746 | 0, /*nb_divmod*/ |
| 1747 | 0, /*nb_power*/ |
| 1748 | 0, /*nb_negative*/ |
| 1749 | 0, /*nb_positive*/ |
| 1750 | 0, /*nb_absolute*/ |
| 1751 | 0, /*nb_nonzero*/ |
| 1752 | 0, /*nb_invert*/ |
| 1753 | 0, /*nb_lshift*/ |
| 1754 | 0, /*nb_rshift*/ |
| 1755 | (binaryfunc)set_and, /*nb_and*/ |
| 1756 | (binaryfunc)set_xor, /*nb_xor*/ |
| 1757 | (binaryfunc)set_or, /*nb_or*/ |
| 1758 | }; |
| 1759 | |
| 1760 | PyDoc_STRVAR(frozenset_doc, |
| 1761 | "frozenset(iterable) --> frozenset object\n\ |
| 1762 | \n\ |
| 1763 | Build an immutable unordered collection."); |
| 1764 | |
| 1765 | PyTypeObject PyFrozenSet_Type = { |
| 1766 | PyObject_HEAD_INIT(&PyType_Type) |
| 1767 | 0, /* ob_size */ |
| 1768 | "frozenset", /* tp_name */ |
| 1769 | sizeof(PySetObject), /* tp_basicsize */ |
Raymond Hettinger | a3b11e7 | 2003-12-31 14:08:58 +0000 | [diff] [blame] | 1770 | 0, /* tp_itemsize */ |
| 1771 | /* methods */ |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1772 | (destructor)set_dealloc, /* tp_dealloc */ |
| 1773 | (printfunc)set_tp_print, /* tp_print */ |
| 1774 | 0, /* tp_getattr */ |
| 1775 | 0, /* tp_setattr */ |
| 1776 | (cmpfunc)set_nocmp, /* tp_compare */ |
| 1777 | (reprfunc)set_repr, /* tp_repr */ |
| 1778 | &frozenset_as_number, /* tp_as_number */ |
| 1779 | &set_as_sequence, /* tp_as_sequence */ |
| 1780 | 0, /* tp_as_mapping */ |
| 1781 | frozenset_hash, /* tp_hash */ |
| 1782 | 0, /* tp_call */ |
| 1783 | 0, /* tp_str */ |
| 1784 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 1785 | 0, /* tp_setattro */ |
| 1786 | 0, /* tp_as_buffer */ |
Raymond Hettinger | bb999b5 | 2005-06-18 21:00:26 +0000 | [diff] [blame] | 1787 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES | |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1788 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1789 | frozenset_doc, /* tp_doc */ |
Raymond Hettinger | bb999b5 | 2005-06-18 21:00:26 +0000 | [diff] [blame] | 1790 | (traverseproc)set_traverse, /* tp_traverse */ |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1791 | 0, /* tp_clear */ |
| 1792 | (richcmpfunc)set_richcompare, /* tp_richcompare */ |
Raymond Hettinger | 691d805 | 2004-05-30 07:26:47 +0000 | [diff] [blame] | 1793 | offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */ |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1794 | (getiterfunc)set_iter, /* tp_iter */ |
| 1795 | 0, /* tp_iternext */ |
| 1796 | frozenset_methods, /* tp_methods */ |
| 1797 | 0, /* tp_members */ |
| 1798 | 0, /* tp_getset */ |
| 1799 | 0, /* tp_base */ |
| 1800 | 0, /* tp_dict */ |
| 1801 | 0, /* tp_descr_get */ |
| 1802 | 0, /* tp_descr_set */ |
| 1803 | 0, /* tp_dictoffset */ |
| 1804 | 0, /* tp_init */ |
| 1805 | PyType_GenericAlloc, /* tp_alloc */ |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 1806 | frozenset_new, /* tp_new */ |
Raymond Hettinger | bb999b5 | 2005-06-18 21:00:26 +0000 | [diff] [blame] | 1807 | PyObject_GC_Del, /* tp_free */ |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1808 | }; |