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); |
Raymond Hettinger | 9dcb17c | 2005-07-31 13:09:28 +0000 | [diff] [blame^] | 738 | Py_INCREF(emptyfrozenset); |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 739 | return emptyfrozenset; |
| 740 | } |
| 741 | } else if (PyFrozenSet_CheckExact(iterable)) { |
Raymond Hettinger | 49ba4c3 | 2003-11-23 02:49:05 +0000 | [diff] [blame] | 742 | Py_INCREF(iterable); |
| 743 | return iterable; |
| 744 | } |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 745 | return make_new_set(type, iterable); |
| 746 | } |
| 747 | |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 748 | static PyObject * |
| 749 | set_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 750 | { |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 751 | return make_new_set(type, NULL); |
| 752 | } |
| 753 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 754 | static void |
| 755 | set_dealloc(PySetObject *so) |
| 756 | { |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 757 | register setentry *ep; |
| 758 | int fill = so->fill; |
| 759 | |
Raymond Hettinger | bb999b5 | 2005-06-18 21:00:26 +0000 | [diff] [blame] | 760 | PyObject_GC_UnTrack(so); |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 761 | Py_TRASHCAN_SAFE_BEGIN(so) |
Raymond Hettinger | 691d805 | 2004-05-30 07:26:47 +0000 | [diff] [blame] | 762 | if (so->weakreflist != NULL) |
| 763 | PyObject_ClearWeakRefs((PyObject *) so); |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 764 | |
| 765 | for (ep = so->table; fill > 0; ep++) { |
| 766 | if (ep->key) { |
| 767 | --fill; |
| 768 | Py_DECREF(ep->key); |
| 769 | } |
| 770 | } |
| 771 | if (so->table != so->smalltable) |
| 772 | PyMem_DEL(so->table); |
| 773 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 774 | so->ob_type->tp_free(so); |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 775 | Py_TRASHCAN_SAFE_END(so) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 776 | } |
| 777 | |
Raymond Hettinger | bb999b5 | 2005-06-18 21:00:26 +0000 | [diff] [blame] | 778 | static int |
| 779 | set_traverse(PySetObject *so, visitproc visit, void *arg) |
| 780 | { |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 781 | int i = 0; |
| 782 | PyObject *pk; |
| 783 | |
| 784 | while (set_next_internal(so, &i, &pk)) |
| 785 | Py_VISIT(pk); |
Raymond Hettinger | bb999b5 | 2005-06-18 21:00:26 +0000 | [diff] [blame] | 786 | return 0; |
| 787 | } |
| 788 | |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 789 | static int |
| 790 | set_len(PyObject *so) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 791 | { |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 792 | return ((PySetObject *)so)->used; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 793 | } |
| 794 | |
Raymond Hettinger | 934d63e | 2005-07-31 01:33:10 +0000 | [diff] [blame] | 795 | /* set_swap_bodies() switches the contents of any two sets by moving their |
| 796 | internal data pointers and, if needed, copying the internal smalltables. |
| 797 | Semantically equivalent to: |
| 798 | |
| 799 | t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t |
| 800 | |
| 801 | The function always succeeds and it leaves both objects in a stable state. |
| 802 | Useful for creating temporary frozensets from sets for membership testing |
| 803 | in __contains__(), discard(), and remove(). Also useful for operations |
| 804 | that update in-place (by allowing an intermediate result to be swapped |
Raymond Hettinger | 9dcb17c | 2005-07-31 13:09:28 +0000 | [diff] [blame^] | 805 | into one of the original inputs). |
Raymond Hettinger | 934d63e | 2005-07-31 01:33:10 +0000 | [diff] [blame] | 806 | */ |
| 807 | |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 808 | static void |
| 809 | set_swap_bodies(PySetObject *a, PySetObject *b) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 810 | { |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 811 | int t; |
| 812 | setentry *u; |
| 813 | setentry *(*f)(PySetObject *so, PyObject *key, long hash); |
| 814 | setentry tab[PySet_MINSIZE]; |
| 815 | long h; |
| 816 | |
| 817 | t = a->fill; a->fill = b->fill; b->fill = t; |
| 818 | t = a->used; a->used = b->used; b->used = t; |
| 819 | t = a->mask; a->mask = b->mask; b->mask = t; |
| 820 | |
| 821 | u = a->table; |
| 822 | if (a->table == a->smalltable) |
| 823 | u = b->smalltable; |
| 824 | a->table = b->table; |
| 825 | if (b->table == b->smalltable) |
| 826 | a->table = a->smalltable; |
| 827 | b->table = u; |
| 828 | |
| 829 | f = a->lookup; a->lookup = b->lookup; b->lookup = f; |
| 830 | |
| 831 | if (a->table == a->smalltable || b->table == b->smalltable) { |
| 832 | memcpy(tab, a->smalltable, sizeof(tab)); |
| 833 | memcpy(a->smalltable, b->smalltable, sizeof(tab)); |
| 834 | memcpy(b->smalltable, tab, sizeof(tab)); |
| 835 | } |
| 836 | |
| 837 | h = a->hash; a->hash = b->hash; b->hash = h; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 838 | } |
| 839 | |
| 840 | static int |
| 841 | set_contains(PySetObject *so, PyObject *key) |
| 842 | { |
Raymond Hettinger | bfd334a | 2003-11-22 03:55:23 +0000 | [diff] [blame] | 843 | PyObject *tmp; |
Raymond Hettinger | 19c2d77 | 2003-11-21 18:36:54 +0000 | [diff] [blame] | 844 | int result; |
| 845 | |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 846 | result = set_contains_internal(so, key); |
Raymond Hettinger | a38123e | 2003-11-24 22:18:49 +0000 | [diff] [blame] | 847 | if (result == -1 && PyAnySet_Check(key)) { |
Raymond Hettinger | 19c2d77 | 2003-11-21 18:36:54 +0000 | [diff] [blame] | 848 | PyErr_Clear(); |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 849 | tmp = make_new_set(&PyFrozenSet_Type, NULL); |
Raymond Hettinger | 19c2d77 | 2003-11-21 18:36:54 +0000 | [diff] [blame] | 850 | if (tmp == NULL) |
| 851 | return -1; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 852 | set_swap_bodies((PySetObject *)tmp, (PySetObject *)key); |
| 853 | result = set_contains_internal(so, tmp); |
| 854 | set_swap_bodies((PySetObject *)tmp, (PySetObject *)key); |
Raymond Hettinger | 19c2d77 | 2003-11-21 18:36:54 +0000 | [diff] [blame] | 855 | Py_DECREF(tmp); |
| 856 | } |
| 857 | return result; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 858 | } |
| 859 | |
| 860 | static PyObject * |
Raymond Hettinger | 8f5cdaa | 2003-12-13 11:26:12 +0000 | [diff] [blame] | 861 | set_direct_contains(PySetObject *so, PyObject *key) |
| 862 | { |
Raymond Hettinger | 8f5cdaa | 2003-12-13 11:26:12 +0000 | [diff] [blame] | 863 | long result; |
| 864 | |
Raymond Hettinger | 438e02d | 2003-12-13 19:38:47 +0000 | [diff] [blame] | 865 | result = set_contains(so, key); |
Raymond Hettinger | 8f5cdaa | 2003-12-13 11:26:12 +0000 | [diff] [blame] | 866 | if (result == -1) |
| 867 | return NULL; |
| 868 | return PyBool_FromLong(result); |
| 869 | } |
| 870 | |
| 871 | PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x."); |
| 872 | |
| 873 | static PyObject * |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 874 | set_copy(PySetObject *so) |
| 875 | { |
Raymond Hettinger | a38123e | 2003-11-24 22:18:49 +0000 | [diff] [blame] | 876 | return make_new_set(so->ob_type, (PyObject *)so); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 877 | } |
| 878 | |
Raymond Hettinger | 49ba4c3 | 2003-11-23 02:49:05 +0000 | [diff] [blame] | 879 | static PyObject * |
| 880 | frozenset_copy(PySetObject *so) |
| 881 | { |
Raymond Hettinger | f5f41bf | 2003-11-24 02:57:33 +0000 | [diff] [blame] | 882 | if (PyFrozenSet_CheckExact(so)) { |
Raymond Hettinger | 49ba4c3 | 2003-11-23 02:49:05 +0000 | [diff] [blame] | 883 | Py_INCREF(so); |
| 884 | return (PyObject *)so; |
| 885 | } |
| 886 | return set_copy(so); |
| 887 | } |
| 888 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 889 | PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set."); |
| 890 | |
| 891 | static PyObject * |
Raymond Hettinger | f5f41bf | 2003-11-24 02:57:33 +0000 | [diff] [blame] | 892 | set_union(PySetObject *so, PyObject *other) |
| 893 | { |
| 894 | PySetObject *result; |
| 895 | PyObject *rv; |
| 896 | |
| 897 | result = (PySetObject *)set_copy(so); |
| 898 | if (result == NULL) |
| 899 | return NULL; |
Raymond Hettinger | a38123e | 2003-11-24 22:18:49 +0000 | [diff] [blame] | 900 | rv = set_update(result, other); |
Raymond Hettinger | f5f41bf | 2003-11-24 02:57:33 +0000 | [diff] [blame] | 901 | if (rv == NULL) { |
| 902 | Py_DECREF(result); |
| 903 | return NULL; |
| 904 | } |
| 905 | Py_DECREF(rv); |
| 906 | return (PyObject *)result; |
| 907 | } |
| 908 | |
| 909 | PyDoc_STRVAR(union_doc, |
| 910 | "Return the union of two sets as a new set.\n\ |
| 911 | \n\ |
| 912 | (i.e. all elements that are in either set.)"); |
| 913 | |
| 914 | static PyObject * |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 915 | set_or(PySetObject *so, PyObject *other) |
| 916 | { |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 917 | if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) { |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 918 | Py_INCREF(Py_NotImplemented); |
| 919 | return Py_NotImplemented; |
| 920 | } |
| 921 | return set_union(so, other); |
| 922 | } |
| 923 | |
| 924 | static PyObject * |
| 925 | set_ior(PySetObject *so, PyObject *other) |
| 926 | { |
| 927 | PyObject *result; |
| 928 | |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 929 | if (!PyAnySet_Check(other)) { |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 930 | Py_INCREF(Py_NotImplemented); |
| 931 | return Py_NotImplemented; |
| 932 | } |
Raymond Hettinger | a38123e | 2003-11-24 22:18:49 +0000 | [diff] [blame] | 933 | result = set_update(so, other); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 934 | if (result == NULL) |
| 935 | return NULL; |
| 936 | Py_DECREF(result); |
| 937 | Py_INCREF(so); |
| 938 | return (PyObject *)so; |
| 939 | } |
| 940 | |
| 941 | static PyObject * |
| 942 | set_intersection(PySetObject *so, PyObject *other) |
| 943 | { |
| 944 | PySetObject *result; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 945 | PyObject *item, *it, *tmp; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 946 | |
| 947 | result = (PySetObject *)make_new_set(so->ob_type, NULL); |
| 948 | if (result == NULL) |
| 949 | return NULL; |
Raymond Hettinger | f5f41bf | 2003-11-24 02:57:33 +0000 | [diff] [blame] | 950 | |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 951 | if (PyAnySet_Check(other) && set_len(other) > set_len((PyObject *)so)) { |
| 952 | tmp = (PyObject *)so; |
| 953 | so = (PySetObject *)other; |
Raymond Hettinger | a3b11e7 | 2003-12-31 14:08:58 +0000 | [diff] [blame] | 954 | other = tmp; |
| 955 | } |
| 956 | |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 957 | if (PyAnySet_Check(other)) { |
Raymond Hettinger | a3b11e7 | 2003-12-31 14:08:58 +0000 | [diff] [blame] | 958 | int pos = 0; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 959 | while (set_next_internal((PySetObject *)other, &pos, &item)) { |
| 960 | if (set_contains_internal(so, item)) { |
| 961 | if (set_add_internal(result, item) == -1) { |
Raymond Hettinger | a3b11e7 | 2003-12-31 14:08:58 +0000 | [diff] [blame] | 962 | Py_DECREF(result); |
| 963 | return NULL; |
| 964 | } |
| 965 | } |
| 966 | } |
| 967 | return (PyObject *)result; |
Raymond Hettinger | f5f41bf | 2003-11-24 02:57:33 +0000 | [diff] [blame] | 968 | } |
| 969 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 970 | it = PyObject_GetIter(other); |
| 971 | if (it == NULL) { |
| 972 | Py_DECREF(result); |
| 973 | return NULL; |
| 974 | } |
| 975 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 976 | while ((item = PyIter_Next(it)) != NULL) { |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 977 | if (set_contains_internal(so, item)) { |
| 978 | if (set_add_internal(result, item) == -1) { |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 979 | Py_DECREF(it); |
| 980 | Py_DECREF(result); |
| 981 | Py_DECREF(item); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 982 | return NULL; |
| 983 | } |
| 984 | } |
| 985 | Py_DECREF(item); |
| 986 | } |
| 987 | Py_DECREF(it); |
| 988 | if (PyErr_Occurred()) { |
| 989 | Py_DECREF(result); |
| 990 | return NULL; |
| 991 | } |
| 992 | return (PyObject *)result; |
| 993 | } |
| 994 | |
| 995 | PyDoc_STRVAR(intersection_doc, |
| 996 | "Return the intersection of two sets as a new set.\n\ |
| 997 | \n\ |
| 998 | (i.e. all elements that are in both sets.)"); |
| 999 | |
| 1000 | static PyObject * |
| 1001 | set_intersection_update(PySetObject *so, PyObject *other) |
| 1002 | { |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1003 | PyObject *tmp; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1004 | |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1005 | tmp = set_intersection(so, other); |
| 1006 | if (tmp == NULL) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1007 | return NULL; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1008 | set_swap_bodies(so, (PySetObject *)tmp); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1009 | Py_DECREF(tmp); |
| 1010 | Py_RETURN_NONE; |
| 1011 | } |
| 1012 | |
| 1013 | PyDoc_STRVAR(intersection_update_doc, |
| 1014 | "Update a set with the intersection of itself and another."); |
| 1015 | |
| 1016 | static PyObject * |
| 1017 | set_and(PySetObject *so, PyObject *other) |
| 1018 | { |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 1019 | if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) { |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1020 | Py_INCREF(Py_NotImplemented); |
| 1021 | return Py_NotImplemented; |
| 1022 | } |
| 1023 | return set_intersection(so, other); |
| 1024 | } |
| 1025 | |
| 1026 | static PyObject * |
| 1027 | set_iand(PySetObject *so, PyObject *other) |
| 1028 | { |
| 1029 | PyObject *result; |
| 1030 | |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 1031 | if (!PyAnySet_Check(other)) { |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1032 | Py_INCREF(Py_NotImplemented); |
| 1033 | return Py_NotImplemented; |
| 1034 | } |
| 1035 | result = set_intersection_update(so, other); |
| 1036 | if (result == NULL) |
| 1037 | return NULL; |
| 1038 | Py_DECREF(result); |
| 1039 | Py_INCREF(so); |
| 1040 | return (PyObject *)so; |
| 1041 | } |
| 1042 | |
| 1043 | static PyObject * |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1044 | set_difference_update(PySetObject *so, PyObject *other) |
| 1045 | { |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1046 | PyObject *item, *it; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1047 | |
| 1048 | it = PyObject_GetIter(other); |
| 1049 | if (it == NULL) |
| 1050 | return NULL; |
| 1051 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1052 | while ((item = PyIter_Next(it)) != NULL) { |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1053 | if (set_discard_internal(so, item) == -1) { |
| 1054 | Py_DECREF(it); |
| 1055 | Py_DECREF(item); |
| 1056 | return NULL; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1057 | } |
| 1058 | Py_DECREF(item); |
| 1059 | } |
| 1060 | Py_DECREF(it); |
| 1061 | if (PyErr_Occurred()) |
| 1062 | return NULL; |
| 1063 | Py_RETURN_NONE; |
| 1064 | } |
| 1065 | |
| 1066 | PyDoc_STRVAR(difference_update_doc, |
| 1067 | "Remove all elements of another set from this set."); |
| 1068 | |
| 1069 | static PyObject * |
Raymond Hettinger | fb4e33a | 2003-12-15 13:23:55 +0000 | [diff] [blame] | 1070 | set_difference(PySetObject *so, PyObject *other) |
| 1071 | { |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1072 | PyObject *tmp, *key, *result; |
Raymond Hettinger | fb4e33a | 2003-12-15 13:23:55 +0000 | [diff] [blame] | 1073 | int pos = 0; |
| 1074 | |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1075 | if (!PyAnySet_Check(other) && !PyDict_Check(other)) { |
Raymond Hettinger | fb4e33a | 2003-12-15 13:23:55 +0000 | [diff] [blame] | 1076 | result = set_copy(so); |
| 1077 | if (result == NULL) |
| 1078 | return result; |
| 1079 | tmp = set_difference_update((PySetObject *)result, other); |
| 1080 | if (tmp != NULL) { |
| 1081 | Py_DECREF(tmp); |
| 1082 | return result; |
| 1083 | } |
| 1084 | Py_DECREF(result); |
| 1085 | return NULL; |
| 1086 | } |
| 1087 | |
| 1088 | result = make_new_set(so->ob_type, NULL); |
| 1089 | if (result == NULL) |
| 1090 | return NULL; |
Raymond Hettinger | fb4e33a | 2003-12-15 13:23:55 +0000 | [diff] [blame] | 1091 | |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1092 | if (PyDict_Check(other)) { |
| 1093 | while (set_next_internal(so, &pos, &key)) { |
| 1094 | if (!PyDict_Contains(other, key)) { |
| 1095 | if (set_add_internal((PySetObject *)result, key) == -1) |
| 1096 | return NULL; |
| 1097 | } |
| 1098 | } |
| 1099 | return result; |
| 1100 | } |
| 1101 | |
| 1102 | while (set_next_internal(so, &pos, &key)) { |
| 1103 | if (!set_contains_internal((PySetObject *)other, key)) { |
| 1104 | if (set_add_internal((PySetObject *)result, key) == -1) |
Raymond Hettinger | fb4e33a | 2003-12-15 13:23:55 +0000 | [diff] [blame] | 1105 | return NULL; |
| 1106 | } |
| 1107 | } |
| 1108 | return result; |
| 1109 | } |
| 1110 | |
| 1111 | PyDoc_STRVAR(difference_doc, |
| 1112 | "Return the difference of two sets as a new set.\n\ |
| 1113 | \n\ |
| 1114 | (i.e. all elements that are in this set but not the other.)"); |
| 1115 | static PyObject * |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1116 | set_sub(PySetObject *so, PyObject *other) |
| 1117 | { |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 1118 | if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) { |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1119 | Py_INCREF(Py_NotImplemented); |
| 1120 | return Py_NotImplemented; |
| 1121 | } |
| 1122 | return set_difference(so, other); |
| 1123 | } |
| 1124 | |
| 1125 | static PyObject * |
| 1126 | set_isub(PySetObject *so, PyObject *other) |
| 1127 | { |
| 1128 | PyObject *result; |
| 1129 | |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 1130 | if (!PyAnySet_Check(other)) { |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1131 | Py_INCREF(Py_NotImplemented); |
| 1132 | return Py_NotImplemented; |
| 1133 | } |
| 1134 | result = set_difference_update(so, other); |
| 1135 | if (result == NULL) |
| 1136 | return NULL; |
| 1137 | Py_DECREF(result); |
| 1138 | Py_INCREF(so); |
| 1139 | return (PyObject *)so; |
| 1140 | } |
| 1141 | |
| 1142 | static PyObject * |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1143 | set_symmetric_difference_update(PySetObject *so, PyObject *other) |
| 1144 | { |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1145 | PySetObject *otherset; |
| 1146 | PyObject *key; |
Raymond Hettinger | dc5ae11 | 2003-12-13 14:46:46 +0000 | [diff] [blame] | 1147 | int pos = 0; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1148 | |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1149 | if (PyDict_Check(other)) { |
| 1150 | PyObject *value; |
| 1151 | int rv; |
| 1152 | while (PyDict_Next(other, &pos, &key, &value)) { |
| 1153 | rv = set_discard_internal(so, key); |
| 1154 | if (rv == -1) |
| 1155 | return NULL; |
| 1156 | if (rv == DISCARD_NOTFOUND) { |
| 1157 | if (set_add_internal(so, key) == -1) |
| 1158 | return NULL; |
| 1159 | } |
| 1160 | } |
| 1161 | Py_RETURN_NONE; |
| 1162 | } |
| 1163 | |
| 1164 | if (PyAnySet_Check(other)) { |
| 1165 | Py_INCREF(other); |
| 1166 | otherset = (PySetObject *)other; |
| 1167 | } else { |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1168 | otherset = (PySetObject *)make_new_set(so->ob_type, other); |
| 1169 | if (otherset == NULL) |
| 1170 | return NULL; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1171 | } |
| 1172 | |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1173 | while (set_next_internal(otherset, &pos, &key)) { |
| 1174 | int rv = set_discard_internal(so, key); |
| 1175 | if (rv == -1) { |
| 1176 | Py_XDECREF(otherset); |
| 1177 | return NULL; |
| 1178 | } |
| 1179 | if (rv == DISCARD_NOTFOUND) { |
| 1180 | if (set_add_internal(so, key) == -1) { |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1181 | Py_XDECREF(otherset); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1182 | return NULL; |
| 1183 | } |
| 1184 | } |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1185 | } |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1186 | Py_DECREF(otherset); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1187 | Py_RETURN_NONE; |
| 1188 | } |
| 1189 | |
| 1190 | PyDoc_STRVAR(symmetric_difference_update_doc, |
| 1191 | "Update a set with the symmetric difference of itself and another."); |
| 1192 | |
| 1193 | static PyObject * |
Raymond Hettinger | f5f41bf | 2003-11-24 02:57:33 +0000 | [diff] [blame] | 1194 | set_symmetric_difference(PySetObject *so, PyObject *other) |
| 1195 | { |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1196 | PyObject *rv; |
| 1197 | PySetObject *otherset; |
Raymond Hettinger | f5f41bf | 2003-11-24 02:57:33 +0000 | [diff] [blame] | 1198 | |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1199 | otherset = (PySetObject *)make_new_set(so->ob_type, other); |
| 1200 | if (otherset == NULL) |
Raymond Hettinger | f5f41bf | 2003-11-24 02:57:33 +0000 | [diff] [blame] | 1201 | return NULL; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1202 | rv = set_symmetric_difference_update(otherset, (PyObject *)so); |
| 1203 | if (rv == NULL) |
| 1204 | return NULL; |
| 1205 | Py_DECREF(rv); |
| 1206 | return (PyObject *)otherset; |
Raymond Hettinger | f5f41bf | 2003-11-24 02:57:33 +0000 | [diff] [blame] | 1207 | } |
| 1208 | |
| 1209 | PyDoc_STRVAR(symmetric_difference_doc, |
| 1210 | "Return the symmetric difference of two sets as a new set.\n\ |
| 1211 | \n\ |
| 1212 | (i.e. all elements that are in exactly one of the sets.)"); |
| 1213 | |
| 1214 | static PyObject * |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1215 | set_xor(PySetObject *so, PyObject *other) |
| 1216 | { |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 1217 | if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) { |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1218 | Py_INCREF(Py_NotImplemented); |
| 1219 | return Py_NotImplemented; |
| 1220 | } |
| 1221 | return set_symmetric_difference(so, other); |
| 1222 | } |
| 1223 | |
| 1224 | static PyObject * |
| 1225 | set_ixor(PySetObject *so, PyObject *other) |
| 1226 | { |
| 1227 | PyObject *result; |
| 1228 | |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 1229 | if (!PyAnySet_Check(other)) { |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1230 | Py_INCREF(Py_NotImplemented); |
| 1231 | return Py_NotImplemented; |
| 1232 | } |
| 1233 | result = set_symmetric_difference_update(so, other); |
| 1234 | if (result == NULL) |
| 1235 | return NULL; |
| 1236 | Py_DECREF(result); |
| 1237 | Py_INCREF(so); |
| 1238 | return (PyObject *)so; |
| 1239 | } |
| 1240 | |
| 1241 | static PyObject * |
| 1242 | set_issubset(PySetObject *so, PyObject *other) |
| 1243 | { |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1244 | PyObject *tmp, *result; |
| 1245 | PyObject *key; |
Raymond Hettinger | dc5ae11 | 2003-12-13 14:46:46 +0000 | [diff] [blame] | 1246 | int pos = 0; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1247 | |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 1248 | if (!PyAnySet_Check(other)) { |
Raymond Hettinger | 3fbec70 | 2003-11-21 07:56:36 +0000 | [diff] [blame] | 1249 | tmp = make_new_set(&PySet_Type, other); |
| 1250 | if (tmp == NULL) |
| 1251 | return NULL; |
| 1252 | result = set_issubset(so, tmp); |
| 1253 | Py_DECREF(tmp); |
| 1254 | return result; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1255 | } |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1256 | if (set_len((PyObject *)so) > set_len(other)) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1257 | Py_RETURN_FALSE; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1258 | |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1259 | while (set_next_internal(so, &pos, &key)) { |
| 1260 | if (!set_contains_internal((PySetObject *)other, key)) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1261 | Py_RETURN_FALSE; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1262 | } |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1263 | Py_RETURN_TRUE; |
| 1264 | } |
| 1265 | |
| 1266 | PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set."); |
| 1267 | |
| 1268 | static PyObject * |
| 1269 | set_issuperset(PySetObject *so, PyObject *other) |
| 1270 | { |
Raymond Hettinger | 3fbec70 | 2003-11-21 07:56:36 +0000 | [diff] [blame] | 1271 | PyObject *tmp, *result; |
| 1272 | |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 1273 | if (!PyAnySet_Check(other)) { |
Raymond Hettinger | 3fbec70 | 2003-11-21 07:56:36 +0000 | [diff] [blame] | 1274 | tmp = make_new_set(&PySet_Type, other); |
| 1275 | if (tmp == NULL) |
| 1276 | return NULL; |
| 1277 | result = set_issuperset(so, tmp); |
| 1278 | Py_DECREF(tmp); |
| 1279 | return result; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1280 | } |
| 1281 | return set_issubset((PySetObject *)other, (PyObject *)so); |
| 1282 | } |
| 1283 | |
| 1284 | PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set."); |
| 1285 | |
| 1286 | static long |
| 1287 | set_nohash(PyObject *self) |
| 1288 | { |
| 1289 | PyErr_SetString(PyExc_TypeError, "set objects are unhashable"); |
| 1290 | return -1; |
| 1291 | } |
| 1292 | |
| 1293 | static int |
| 1294 | set_nocmp(PyObject *self) |
| 1295 | { |
| 1296 | PyErr_SetString(PyExc_TypeError, "cannot compare sets using cmp()"); |
| 1297 | return -1; |
| 1298 | } |
| 1299 | |
| 1300 | static long |
| 1301 | frozenset_hash(PyObject *self) |
| 1302 | { |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1303 | PyObject *key; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1304 | PySetObject *so = (PySetObject *)self; |
Raymond Hettinger | dc5ae11 | 2003-12-13 14:46:46 +0000 | [diff] [blame] | 1305 | int pos = 0; |
Raymond Hettinger | c978633 | 2004-06-10 22:41:48 +0000 | [diff] [blame] | 1306 | long hash = 1927868237L; |
Raymond Hettinger | 438e02d | 2003-12-13 19:38:47 +0000 | [diff] [blame] | 1307 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1308 | if (so->hash != -1) |
| 1309 | return so->hash; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1310 | |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1311 | hash *= set_len(self) + 1; |
| 1312 | while (set_next_internal(so, &pos, &key)) { |
Raymond Hettinger | c978633 | 2004-06-10 22:41:48 +0000 | [diff] [blame] | 1313 | /* Work to increase the bit dispersion for closely spaced hash |
| 1314 | values. The is important because some use cases have many |
| 1315 | combinations of a small number of elements with nearby |
| 1316 | hashes so that many distinct combinations collapse to only |
| 1317 | a handful of distinct hash values. */ |
| 1318 | long h = PyObject_Hash(key); |
| 1319 | hash ^= (h ^ (h << 16) ^ 89869747L) * 3644798167u; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1320 | } |
Raymond Hettinger | c978633 | 2004-06-10 22:41:48 +0000 | [diff] [blame] | 1321 | hash = hash * 69069L + 907133923L; |
Raymond Hettinger | 27e403e | 2004-06-10 21:38:41 +0000 | [diff] [blame] | 1322 | if (hash == -1) |
| 1323 | hash = 590923713L; |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 1324 | so->hash = hash; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1325 | return hash; |
| 1326 | } |
| 1327 | |
| 1328 | static PyObject * |
| 1329 | set_richcompare(PySetObject *v, PyObject *w, int op) |
| 1330 | { |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1331 | PyObject *r1, *r2; |
| 1332 | |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 1333 | if(!PyAnySet_Check(w)) { |
| 1334 | if (op == Py_EQ) |
| 1335 | Py_RETURN_FALSE; |
| 1336 | if (op == Py_NE) |
| 1337 | Py_RETURN_TRUE; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1338 | PyErr_SetString(PyExc_TypeError, "can only compare to a set"); |
| 1339 | return NULL; |
| 1340 | } |
| 1341 | switch (op) { |
| 1342 | case Py_EQ: |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1343 | if (set_len((PyObject *)v) != set_len(w)) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1344 | Py_RETURN_FALSE; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1345 | return set_issubset(v, w); |
| 1346 | case Py_NE: |
| 1347 | if (set_len((PyObject *)v) != set_len(w)) |
| 1348 | Py_RETURN_TRUE; |
| 1349 | r1 = set_issubset(v, w); |
| 1350 | assert (r1 != NULL); |
| 1351 | r2 = PyBool_FromLong(PyObject_Not(r1)); |
| 1352 | Py_DECREF(r1); |
| 1353 | return r2; |
| 1354 | case Py_LE: |
| 1355 | return set_issubset(v, w); |
| 1356 | case Py_GE: |
| 1357 | return set_issuperset(v, w); |
| 1358 | case Py_LT: |
| 1359 | if (set_len((PyObject *)v) >= set_len(w)) |
| 1360 | Py_RETURN_FALSE; |
| 1361 | return set_issubset(v, w); |
| 1362 | case Py_GT: |
| 1363 | if (set_len((PyObject *)v) <= set_len(w)) |
| 1364 | Py_RETURN_FALSE; |
| 1365 | return set_issuperset(v, w); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1366 | } |
| 1367 | Py_INCREF(Py_NotImplemented); |
| 1368 | return Py_NotImplemented; |
| 1369 | } |
| 1370 | |
| 1371 | static PyObject * |
| 1372 | set_repr(PySetObject *so) |
| 1373 | { |
| 1374 | PyObject *keys, *result, *listrepr; |
| 1375 | |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1376 | keys = PySequence_List((PyObject *)so); |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 1377 | if (keys == NULL) |
| 1378 | return NULL; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1379 | listrepr = PyObject_Repr(keys); |
| 1380 | Py_DECREF(keys); |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 1381 | if (listrepr == NULL) |
| 1382 | return NULL; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1383 | |
| 1384 | result = PyString_FromFormat("%s(%s)", so->ob_type->tp_name, |
| 1385 | PyString_AS_STRING(listrepr)); |
| 1386 | Py_DECREF(listrepr); |
| 1387 | return result; |
| 1388 | } |
| 1389 | |
| 1390 | static int |
| 1391 | set_tp_print(PySetObject *so, FILE *fp, int flags) |
| 1392 | { |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1393 | PyObject *key; |
Raymond Hettinger | a3b11e7 | 2003-12-31 14:08:58 +0000 | [diff] [blame] | 1394 | int pos=0; |
| 1395 | char *emit = ""; /* No separator emitted on first pass */ |
| 1396 | char *separator = ", "; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1397 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1398 | fprintf(fp, "%s([", so->ob_type->tp_name); |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1399 | while (set_next_internal(so, &pos, &key)) { |
Raymond Hettinger | a3b11e7 | 2003-12-31 14:08:58 +0000 | [diff] [blame] | 1400 | fputs(emit, fp); |
| 1401 | emit = separator; |
Raymond Hettinger | dc5ae11 | 2003-12-13 14:46:46 +0000 | [diff] [blame] | 1402 | if (PyObject_Print(key, fp, 0) != 0) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1403 | return -1; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1404 | } |
Raymond Hettinger | a3b11e7 | 2003-12-31 14:08:58 +0000 | [diff] [blame] | 1405 | fputs("])", fp); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1406 | return 0; |
| 1407 | } |
| 1408 | |
| 1409 | static PyObject * |
| 1410 | set_clear(PySetObject *so) |
| 1411 | { |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1412 | set_clear_internal(so); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1413 | so->hash = -1; |
Raymond Hettinger | 438e02d | 2003-12-13 19:38:47 +0000 | [diff] [blame] | 1414 | Py_RETURN_NONE; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1415 | } |
| 1416 | |
| 1417 | PyDoc_STRVAR(clear_doc, "Remove all elements from this set."); |
| 1418 | |
Raymond Hettinger | bb999b5 | 2005-06-18 21:00:26 +0000 | [diff] [blame] | 1419 | static int |
| 1420 | set_tp_clear(PySetObject *so) |
| 1421 | { |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1422 | set_clear_internal(so); |
Raymond Hettinger | bb999b5 | 2005-06-18 21:00:26 +0000 | [diff] [blame] | 1423 | so->hash = -1; |
| 1424 | return 0; |
| 1425 | } |
| 1426 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1427 | static PyObject * |
| 1428 | set_add(PySetObject *so, PyObject *item) |
| 1429 | { |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1430 | if (set_add_internal(so, item) == -1) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1431 | return NULL; |
Raymond Hettinger | 438e02d | 2003-12-13 19:38:47 +0000 | [diff] [blame] | 1432 | Py_RETURN_NONE; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1433 | } |
| 1434 | |
| 1435 | PyDoc_STRVAR(add_doc, |
| 1436 | "Add an element to a set.\n\ |
| 1437 | \n\ |
| 1438 | This has no effect if the element is already present."); |
| 1439 | |
| 1440 | static PyObject * |
| 1441 | set_remove(PySetObject *so, PyObject *item) |
| 1442 | { |
Raymond Hettinger | 0deab62 | 2003-12-13 18:53:18 +0000 | [diff] [blame] | 1443 | PyObject *tmp, *result; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1444 | int rv; |
Raymond Hettinger | bfd334a | 2003-11-22 03:55:23 +0000 | [diff] [blame] | 1445 | |
Raymond Hettinger | 0deab62 | 2003-12-13 18:53:18 +0000 | [diff] [blame] | 1446 | if (PyType_IsSubtype(item->ob_type, &PySet_Type)) { |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1447 | tmp = make_new_set(&PyFrozenSet_Type, NULL); |
Raymond Hettinger | bfd334a | 2003-11-22 03:55:23 +0000 | [diff] [blame] | 1448 | if (tmp == NULL) |
| 1449 | return NULL; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1450 | set_swap_bodies((PySetObject *)item, (PySetObject *)tmp); |
Raymond Hettinger | 0deab62 | 2003-12-13 18:53:18 +0000 | [diff] [blame] | 1451 | result = set_remove(so, tmp); |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1452 | set_swap_bodies((PySetObject *)item, (PySetObject *)tmp); |
Raymond Hettinger | bfd334a | 2003-11-22 03:55:23 +0000 | [diff] [blame] | 1453 | Py_DECREF(tmp); |
Raymond Hettinger | 0deab62 | 2003-12-13 18:53:18 +0000 | [diff] [blame] | 1454 | return result; |
Raymond Hettinger | bfd334a | 2003-11-22 03:55:23 +0000 | [diff] [blame] | 1455 | } |
Raymond Hettinger | 0deab62 | 2003-12-13 18:53:18 +0000 | [diff] [blame] | 1456 | |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1457 | rv = set_discard_internal(so, item); |
| 1458 | if (rv == -1) |
Raymond Hettinger | 0deab62 | 2003-12-13 18:53:18 +0000 | [diff] [blame] | 1459 | return NULL; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1460 | else if (rv == DISCARD_NOTFOUND) { |
| 1461 | PyErr_SetObject(PyExc_KeyError, item); |
| 1462 | return NULL; |
| 1463 | } |
Raymond Hettinger | 438e02d | 2003-12-13 19:38:47 +0000 | [diff] [blame] | 1464 | Py_RETURN_NONE; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1465 | } |
| 1466 | |
| 1467 | PyDoc_STRVAR(remove_doc, |
| 1468 | "Remove an element from a set; it must be a member.\n\ |
| 1469 | \n\ |
| 1470 | If the element is not a member, raise a KeyError."); |
| 1471 | |
| 1472 | static PyObject * |
| 1473 | set_discard(PySetObject *so, PyObject *item) |
| 1474 | { |
Raymond Hettinger | 0deab62 | 2003-12-13 18:53:18 +0000 | [diff] [blame] | 1475 | PyObject *tmp, *result; |
| 1476 | |
| 1477 | if (PyType_IsSubtype(item->ob_type, &PySet_Type)) { |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1478 | tmp = make_new_set(&PyFrozenSet_Type, NULL); |
Raymond Hettinger | 0deab62 | 2003-12-13 18:53:18 +0000 | [diff] [blame] | 1479 | if (tmp == NULL) |
| 1480 | return NULL; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1481 | set_swap_bodies((PySetObject *)item, (PySetObject *)tmp); |
Raymond Hettinger | 0deab62 | 2003-12-13 18:53:18 +0000 | [diff] [blame] | 1482 | result = set_discard(so, tmp); |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1483 | set_swap_bodies((PySetObject *)item, (PySetObject *)tmp); |
Raymond Hettinger | 0deab62 | 2003-12-13 18:53:18 +0000 | [diff] [blame] | 1484 | Py_DECREF(tmp); |
| 1485 | return result; |
| 1486 | } |
Raymond Hettinger | bfd334a | 2003-11-22 03:55:23 +0000 | [diff] [blame] | 1487 | |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1488 | if (set_discard_internal(so, item) == -1) |
| 1489 | return NULL; |
Raymond Hettinger | 438e02d | 2003-12-13 19:38:47 +0000 | [diff] [blame] | 1490 | Py_RETURN_NONE; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1491 | } |
| 1492 | |
| 1493 | PyDoc_STRVAR(discard_doc, |
| 1494 | "Remove an element from a set if it is a member.\n\ |
| 1495 | \n\ |
| 1496 | If the element is not a member, do nothing."); |
| 1497 | |
| 1498 | static PyObject * |
| 1499 | set_pop(PySetObject *so) |
| 1500 | { |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1501 | PyObject *key; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1502 | int pos = 0; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1503 | int rv; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1504 | |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1505 | if (!set_next_internal(so, &pos, &key)) { |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1506 | PyErr_SetString(PyExc_KeyError, "pop from an empty set"); |
| 1507 | return NULL; |
| 1508 | } |
| 1509 | Py_INCREF(key); |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1510 | |
| 1511 | rv = set_discard_internal(so, key); |
| 1512 | if (rv == -1) { |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 1513 | Py_DECREF(key); |
| 1514 | return NULL; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1515 | } else if (rv == DISCARD_NOTFOUND) { |
| 1516 | Py_DECREF(key); |
| 1517 | PyErr_SetObject(PyExc_KeyError, key); |
| 1518 | return NULL; |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 1519 | } |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1520 | return key; |
| 1521 | } |
| 1522 | |
| 1523 | PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element."); |
| 1524 | |
| 1525 | static PyObject * |
| 1526 | set_reduce(PySetObject *so) |
| 1527 | { |
Raymond Hettinger | 15056a5 | 2004-11-09 07:25:31 +0000 | [diff] [blame] | 1528 | PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1529 | |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1530 | keys = PySequence_List((PyObject *)so); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1531 | if (keys == NULL) |
| 1532 | goto done; |
| 1533 | args = PyTuple_Pack(1, keys); |
| 1534 | if (args == NULL) |
| 1535 | goto done; |
Raymond Hettinger | 15056a5 | 2004-11-09 07:25:31 +0000 | [diff] [blame] | 1536 | dict = PyObject_GetAttrString((PyObject *)so, "__dict__"); |
| 1537 | if (dict == NULL) { |
| 1538 | PyErr_Clear(); |
| 1539 | dict = Py_None; |
| 1540 | Py_INCREF(dict); |
| 1541 | } |
| 1542 | result = PyTuple_Pack(3, so->ob_type, args, dict); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1543 | done: |
| 1544 | Py_XDECREF(args); |
| 1545 | Py_XDECREF(keys); |
Raymond Hettinger | 15056a5 | 2004-11-09 07:25:31 +0000 | [diff] [blame] | 1546 | Py_XDECREF(dict); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1547 | return result; |
| 1548 | } |
| 1549 | |
| 1550 | PyDoc_STRVAR(reduce_doc, "Return state information for pickling."); |
| 1551 | |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 1552 | static int |
| 1553 | set_init(PySetObject *self, PyObject *args, PyObject *kwds) |
| 1554 | { |
| 1555 | PyObject *iterable = NULL; |
| 1556 | PyObject *result; |
| 1557 | |
| 1558 | if (!PyAnySet_Check(self)) |
| 1559 | return -1; |
| 1560 | if (!PyArg_UnpackTuple(args, self->ob_type->tp_name, 0, 1, &iterable)) |
| 1561 | return -1; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1562 | set_clear_internal(self); |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 1563 | self->hash = -1; |
| 1564 | if (iterable == NULL) |
| 1565 | return 0; |
Raymond Hettinger | a38123e | 2003-11-24 22:18:49 +0000 | [diff] [blame] | 1566 | result = set_update(self, iterable); |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 1567 | if (result != NULL) { |
| 1568 | Py_DECREF(result); |
| 1569 | return 0; |
| 1570 | } |
| 1571 | return -1; |
| 1572 | } |
| 1573 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1574 | static PySequenceMethods set_as_sequence = { |
| 1575 | (inquiry)set_len, /* sq_length */ |
| 1576 | 0, /* sq_concat */ |
| 1577 | 0, /* sq_repeat */ |
| 1578 | 0, /* sq_item */ |
| 1579 | 0, /* sq_slice */ |
| 1580 | 0, /* sq_ass_item */ |
| 1581 | 0, /* sq_ass_slice */ |
| 1582 | (objobjproc)set_contains, /* sq_contains */ |
| 1583 | }; |
| 1584 | |
| 1585 | /* set object ********************************************************/ |
| 1586 | |
| 1587 | static PyMethodDef set_methods[] = { |
| 1588 | {"add", (PyCFunction)set_add, METH_O, |
| 1589 | add_doc}, |
| 1590 | {"clear", (PyCFunction)set_clear, METH_NOARGS, |
| 1591 | clear_doc}, |
Raymond Hettinger | 0deab62 | 2003-12-13 18:53:18 +0000 | [diff] [blame] | 1592 | {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST, |
Raymond Hettinger | 8f5cdaa | 2003-12-13 11:26:12 +0000 | [diff] [blame] | 1593 | contains_doc}, |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1594 | {"copy", (PyCFunction)set_copy, METH_NOARGS, |
| 1595 | copy_doc}, |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1596 | {"discard", (PyCFunction)set_discard, METH_O, |
| 1597 | discard_doc}, |
| 1598 | {"difference", (PyCFunction)set_difference, METH_O, |
| 1599 | difference_doc}, |
| 1600 | {"difference_update", (PyCFunction)set_difference_update, METH_O, |
| 1601 | difference_update_doc}, |
| 1602 | {"intersection",(PyCFunction)set_intersection, METH_O, |
| 1603 | intersection_doc}, |
| 1604 | {"intersection_update",(PyCFunction)set_intersection_update, METH_O, |
| 1605 | intersection_update_doc}, |
| 1606 | {"issubset", (PyCFunction)set_issubset, METH_O, |
| 1607 | issubset_doc}, |
| 1608 | {"issuperset", (PyCFunction)set_issuperset, METH_O, |
| 1609 | issuperset_doc}, |
| 1610 | {"pop", (PyCFunction)set_pop, METH_NOARGS, |
| 1611 | pop_doc}, |
| 1612 | {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS, |
| 1613 | reduce_doc}, |
| 1614 | {"remove", (PyCFunction)set_remove, METH_O, |
| 1615 | remove_doc}, |
| 1616 | {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O, |
| 1617 | symmetric_difference_doc}, |
| 1618 | {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O, |
| 1619 | symmetric_difference_update_doc}, |
| 1620 | {"union", (PyCFunction)set_union, METH_O, |
| 1621 | union_doc}, |
Raymond Hettinger | a38123e | 2003-11-24 22:18:49 +0000 | [diff] [blame] | 1622 | {"update", (PyCFunction)set_update, METH_O, |
| 1623 | update_doc}, |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1624 | {NULL, NULL} /* sentinel */ |
| 1625 | }; |
| 1626 | |
| 1627 | static PyNumberMethods set_as_number = { |
| 1628 | 0, /*nb_add*/ |
| 1629 | (binaryfunc)set_sub, /*nb_subtract*/ |
| 1630 | 0, /*nb_multiply*/ |
| 1631 | 0, /*nb_divide*/ |
| 1632 | 0, /*nb_remainder*/ |
| 1633 | 0, /*nb_divmod*/ |
| 1634 | 0, /*nb_power*/ |
| 1635 | 0, /*nb_negative*/ |
| 1636 | 0, /*nb_positive*/ |
| 1637 | 0, /*nb_absolute*/ |
| 1638 | 0, /*nb_nonzero*/ |
| 1639 | 0, /*nb_invert*/ |
| 1640 | 0, /*nb_lshift*/ |
| 1641 | 0, /*nb_rshift*/ |
| 1642 | (binaryfunc)set_and, /*nb_and*/ |
| 1643 | (binaryfunc)set_xor, /*nb_xor*/ |
| 1644 | (binaryfunc)set_or, /*nb_or*/ |
| 1645 | 0, /*nb_coerce*/ |
| 1646 | 0, /*nb_int*/ |
| 1647 | 0, /*nb_long*/ |
| 1648 | 0, /*nb_float*/ |
| 1649 | 0, /*nb_oct*/ |
| 1650 | 0, /*nb_hex*/ |
| 1651 | 0, /*nb_inplace_add*/ |
| 1652 | (binaryfunc)set_isub, /*nb_inplace_subtract*/ |
| 1653 | 0, /*nb_inplace_multiply*/ |
| 1654 | 0, /*nb_inplace_divide*/ |
| 1655 | 0, /*nb_inplace_remainder*/ |
| 1656 | 0, /*nb_inplace_power*/ |
| 1657 | 0, /*nb_inplace_lshift*/ |
| 1658 | 0, /*nb_inplace_rshift*/ |
| 1659 | (binaryfunc)set_iand, /*nb_inplace_and*/ |
| 1660 | (binaryfunc)set_ixor, /*nb_inplace_xor*/ |
| 1661 | (binaryfunc)set_ior, /*nb_inplace_or*/ |
| 1662 | }; |
| 1663 | |
| 1664 | PyDoc_STRVAR(set_doc, |
| 1665 | "set(iterable) --> set object\n\ |
| 1666 | \n\ |
| 1667 | Build an unordered collection."); |
| 1668 | |
| 1669 | PyTypeObject PySet_Type = { |
| 1670 | PyObject_HEAD_INIT(&PyType_Type) |
| 1671 | 0, /* ob_size */ |
| 1672 | "set", /* tp_name */ |
| 1673 | sizeof(PySetObject), /* tp_basicsize */ |
| 1674 | 0, /* tp_itemsize */ |
| 1675 | /* methods */ |
| 1676 | (destructor)set_dealloc, /* tp_dealloc */ |
| 1677 | (printfunc)set_tp_print, /* tp_print */ |
| 1678 | 0, /* tp_getattr */ |
| 1679 | 0, /* tp_setattr */ |
| 1680 | (cmpfunc)set_nocmp, /* tp_compare */ |
| 1681 | (reprfunc)set_repr, /* tp_repr */ |
| 1682 | &set_as_number, /* tp_as_number */ |
| 1683 | &set_as_sequence, /* tp_as_sequence */ |
| 1684 | 0, /* tp_as_mapping */ |
| 1685 | set_nohash, /* tp_hash */ |
| 1686 | 0, /* tp_call */ |
| 1687 | 0, /* tp_str */ |
| 1688 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 1689 | 0, /* tp_setattro */ |
| 1690 | 0, /* tp_as_buffer */ |
Raymond Hettinger | bb999b5 | 2005-06-18 21:00:26 +0000 | [diff] [blame] | 1691 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES | |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1692 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1693 | set_doc, /* tp_doc */ |
Raymond Hettinger | bb999b5 | 2005-06-18 21:00:26 +0000 | [diff] [blame] | 1694 | (traverseproc)set_traverse, /* tp_traverse */ |
| 1695 | (inquiry)set_tp_clear, /* tp_clear */ |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1696 | (richcmpfunc)set_richcompare, /* tp_richcompare */ |
Raymond Hettinger | 691d805 | 2004-05-30 07:26:47 +0000 | [diff] [blame] | 1697 | offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */ |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1698 | (getiterfunc)set_iter, /* tp_iter */ |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1699 | 0, /* tp_iternext */ |
| 1700 | set_methods, /* tp_methods */ |
| 1701 | 0, /* tp_members */ |
| 1702 | 0, /* tp_getset */ |
| 1703 | 0, /* tp_base */ |
| 1704 | 0, /* tp_dict */ |
| 1705 | 0, /* tp_descr_get */ |
| 1706 | 0, /* tp_descr_set */ |
| 1707 | 0, /* tp_dictoffset */ |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 1708 | (initproc)set_init, /* tp_init */ |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1709 | PyType_GenericAlloc, /* tp_alloc */ |
| 1710 | set_new, /* tp_new */ |
Raymond Hettinger | bb999b5 | 2005-06-18 21:00:26 +0000 | [diff] [blame] | 1711 | PyObject_GC_Del, /* tp_free */ |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1712 | }; |
| 1713 | |
| 1714 | /* frozenset object ********************************************************/ |
| 1715 | |
| 1716 | |
| 1717 | static PyMethodDef frozenset_methods[] = { |
Raymond Hettinger | 0deab62 | 2003-12-13 18:53:18 +0000 | [diff] [blame] | 1718 | {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST, |
Raymond Hettinger | 8f5cdaa | 2003-12-13 11:26:12 +0000 | [diff] [blame] | 1719 | contains_doc}, |
Raymond Hettinger | 49ba4c3 | 2003-11-23 02:49:05 +0000 | [diff] [blame] | 1720 | {"copy", (PyCFunction)frozenset_copy, METH_NOARGS, |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1721 | copy_doc}, |
Raymond Hettinger | 49ba4c3 | 2003-11-23 02:49:05 +0000 | [diff] [blame] | 1722 | {"difference", (PyCFunction)set_difference, METH_O, |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1723 | difference_doc}, |
| 1724 | {"intersection",(PyCFunction)set_intersection, METH_O, |
| 1725 | intersection_doc}, |
Raymond Hettinger | 49ba4c3 | 2003-11-23 02:49:05 +0000 | [diff] [blame] | 1726 | {"issubset", (PyCFunction)set_issubset, METH_O, |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1727 | issubset_doc}, |
Raymond Hettinger | 49ba4c3 | 2003-11-23 02:49:05 +0000 | [diff] [blame] | 1728 | {"issuperset", (PyCFunction)set_issuperset, METH_O, |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1729 | issuperset_doc}, |
| 1730 | {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS, |
| 1731 | reduce_doc}, |
| 1732 | {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O, |
| 1733 | symmetric_difference_doc}, |
| 1734 | {"union", (PyCFunction)set_union, METH_O, |
| 1735 | union_doc}, |
| 1736 | {NULL, NULL} /* sentinel */ |
| 1737 | }; |
| 1738 | |
| 1739 | static PyNumberMethods frozenset_as_number = { |
| 1740 | 0, /*nb_add*/ |
| 1741 | (binaryfunc)set_sub, /*nb_subtract*/ |
| 1742 | 0, /*nb_multiply*/ |
| 1743 | 0, /*nb_divide*/ |
| 1744 | 0, /*nb_remainder*/ |
| 1745 | 0, /*nb_divmod*/ |
| 1746 | 0, /*nb_power*/ |
| 1747 | 0, /*nb_negative*/ |
| 1748 | 0, /*nb_positive*/ |
| 1749 | 0, /*nb_absolute*/ |
| 1750 | 0, /*nb_nonzero*/ |
| 1751 | 0, /*nb_invert*/ |
| 1752 | 0, /*nb_lshift*/ |
| 1753 | 0, /*nb_rshift*/ |
| 1754 | (binaryfunc)set_and, /*nb_and*/ |
| 1755 | (binaryfunc)set_xor, /*nb_xor*/ |
| 1756 | (binaryfunc)set_or, /*nb_or*/ |
| 1757 | }; |
| 1758 | |
| 1759 | PyDoc_STRVAR(frozenset_doc, |
| 1760 | "frozenset(iterable) --> frozenset object\n\ |
| 1761 | \n\ |
| 1762 | Build an immutable unordered collection."); |
| 1763 | |
| 1764 | PyTypeObject PyFrozenSet_Type = { |
| 1765 | PyObject_HEAD_INIT(&PyType_Type) |
| 1766 | 0, /* ob_size */ |
| 1767 | "frozenset", /* tp_name */ |
| 1768 | sizeof(PySetObject), /* tp_basicsize */ |
Raymond Hettinger | a3b11e7 | 2003-12-31 14:08:58 +0000 | [diff] [blame] | 1769 | 0, /* tp_itemsize */ |
| 1770 | /* methods */ |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1771 | (destructor)set_dealloc, /* tp_dealloc */ |
| 1772 | (printfunc)set_tp_print, /* tp_print */ |
| 1773 | 0, /* tp_getattr */ |
| 1774 | 0, /* tp_setattr */ |
| 1775 | (cmpfunc)set_nocmp, /* tp_compare */ |
| 1776 | (reprfunc)set_repr, /* tp_repr */ |
| 1777 | &frozenset_as_number, /* tp_as_number */ |
| 1778 | &set_as_sequence, /* tp_as_sequence */ |
| 1779 | 0, /* tp_as_mapping */ |
| 1780 | frozenset_hash, /* tp_hash */ |
| 1781 | 0, /* tp_call */ |
| 1782 | 0, /* tp_str */ |
| 1783 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 1784 | 0, /* tp_setattro */ |
| 1785 | 0, /* tp_as_buffer */ |
Raymond Hettinger | bb999b5 | 2005-06-18 21:00:26 +0000 | [diff] [blame] | 1786 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES | |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1787 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1788 | frozenset_doc, /* tp_doc */ |
Raymond Hettinger | bb999b5 | 2005-06-18 21:00:26 +0000 | [diff] [blame] | 1789 | (traverseproc)set_traverse, /* tp_traverse */ |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1790 | 0, /* tp_clear */ |
| 1791 | (richcmpfunc)set_richcompare, /* tp_richcompare */ |
Raymond Hettinger | 691d805 | 2004-05-30 07:26:47 +0000 | [diff] [blame] | 1792 | offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */ |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1793 | (getiterfunc)set_iter, /* tp_iter */ |
| 1794 | 0, /* tp_iternext */ |
| 1795 | frozenset_methods, /* tp_methods */ |
| 1796 | 0, /* tp_members */ |
| 1797 | 0, /* tp_getset */ |
| 1798 | 0, /* tp_base */ |
| 1799 | 0, /* tp_dict */ |
| 1800 | 0, /* tp_descr_get */ |
| 1801 | 0, /* tp_descr_set */ |
| 1802 | 0, /* tp_dictoffset */ |
| 1803 | 0, /* tp_init */ |
| 1804 | PyType_GenericAlloc, /* tp_alloc */ |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 1805 | frozenset_new, /* tp_new */ |
Raymond Hettinger | bb999b5 | 2005-06-18 21:00:26 +0000 | [diff] [blame] | 1806 | PyObject_GC_Del, /* tp_free */ |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1807 | }; |