blob: e922b6ef539891ee725d3fc4c67aa6db3d9c877d [file] [log] [blame]
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001
2/* Set object implementation using a hash table
3 Functions adapted from dictobject.c
4*/
5
Raymond Hettingera690a992003-11-16 16:17:49 +00006#include "Python.h"
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00007
8/* This must be >= 1. */
9#define PERTURB_SHIFT 5
10
11/* Object used as dummy key to fill deleted entries */
12static 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/*
23The basic lookup function used by all operations.
24This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
25Open addressing is preferred over chaining since the link overhead for
26chaining would be substantial (100% with typical malloc overhead).
27
28The initial probe index is computed as hash mod the table size. Subsequent
29probe indices are computed as explained earlier.
30
31All arithmetic on hash should ignore overflow.
32
33This function must never return NULL; failures are indicated by returning
34a setentry* for which the value field is NULL. Exceptions are never
35reported by this function, and outstanding exceptions are maintained.
36*/
37
38static setentry *
39set_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
129Done:
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 */
145static setentry *
146set_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/*
193Internal routine to insert a new item into the table.
194Used both by the internal resize routine and by the public insert routine.
195Eats a reference to key.
196*/
197static void
198set_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/*
225Restructure the table by allocating a new table and reinserting all
226items again. When entries have been deleted, the new table may
227actually be smaller than the old one.
228*/
229static int
230set_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 */
318static int
319set_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
345static int
346set_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
370static void
371set_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 */
448static int
449set_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
472static int
473set_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
505static int
506set_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
520static PyTypeObject PySetIter_Type; /* Forward */
521
522/* Set iterator types */
523
524typedef 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
532static PyObject *
533set_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
546static void
547setiter_dealloc(setiterobject *si)
548{
549 Py_XDECREF(si->si_set);
550 PyObject_Del(si);
551}
552
553static int
554setiter_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
561static PySequenceMethods setiter_as_sequence = {
562 (inquiry)setiter_len, /* sq_length */
563 0, /* sq_concat */
564};
565
566static 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
599fail:
600 Py_DECREF(d);
601 si->si_set = NULL;
602 return NULL;
603}
604
605PyTypeObject 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 Hettinger691d8052004-05-30 07:26:47 +0000639#include "structmember.h"
Raymond Hettingera690a992003-11-16 16:17:49 +0000640
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 Hettinger9f1a6792005-07-31 01:16:36 +0000646 Copyright (c) 2003-5 Python Software Foundation.
Raymond Hettingera690a992003-11-16 16:17:49 +0000647 All rights reserved.
648*/
649
Raymond Hettingera690a992003-11-16 16:17:49 +0000650static PyObject *
Raymond Hettingera38123e2003-11-24 22:18:49 +0000651set_update(PySetObject *so, PyObject *other)
Raymond Hettingera690a992003-11-16 16:17:49 +0000652{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000653 PyObject *item, *it;
Raymond Hettingera690a992003-11-16 16:17:49 +0000654
Raymond Hettingera38123e2003-11-24 22:18:49 +0000655 if (PyAnySet_Check(other)) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000656 if (set_merge_internal(so, other) == -1)
Raymond Hettingera38123e2003-11-24 22:18:49 +0000657 return NULL;
658 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +0000659 }
660
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000661 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 Hettingera38123e2003-11-24 22:18:49 +0000671 it = PyObject_GetIter(other);
672 if (it == NULL)
673 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +0000674
Raymond Hettingera38123e2003-11-24 22:18:49 +0000675 while ((item = PyIter_Next(it)) != NULL) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000676 if (set_add_internal(so, item) == -1) {
Raymond Hettingera38123e2003-11-24 22:18:49 +0000677 Py_DECREF(it);
Raymond Hettingera690a992003-11-16 16:17:49 +0000678 Py_DECREF(item);
Raymond Hettingera38123e2003-11-24 22:18:49 +0000679 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +0000680 }
681 Py_DECREF(item);
682 }
Raymond Hettingera38123e2003-11-24 22:18:49 +0000683 Py_DECREF(it);
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000684 if (PyErr_Occurred())
Raymond Hettingera38123e2003-11-24 22:18:49 +0000685 return NULL;
686 Py_RETURN_NONE;
687}
688
689PyDoc_STRVAR(update_doc,
690"Update a set with the union of itself and another.");
691
692static PyObject *
693make_new_set(PyTypeObject *type, PyObject *iterable)
694{
Raymond Hettingera38123e2003-11-24 22:18:49 +0000695 PyObject *tmp;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000696 register PySetObject *so = NULL;
Raymond Hettingera38123e2003-11-24 22:18:49 +0000697
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000698 if (dummy == NULL) { /* Auto-initialize dummy */
699 dummy = PyString_FromString("<dummy key>");
700 if (dummy == NULL)
701 return NULL;
702 }
Raymond Hettingera690a992003-11-16 16:17:49 +0000703
704 /* create PySetObject structure */
705 so = (PySetObject *)type->tp_alloc(type, 0);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000706 if (so == NULL)
Raymond Hettingera38123e2003-11-24 22:18:49 +0000707 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000708
709 EMPTY_TO_MINSIZE(so);
710 so->lookup = set_lookkey_string;
Raymond Hettingera690a992003-11-16 16:17:49 +0000711 so->hash = -1;
Raymond Hettinger691d8052004-05-30 07:26:47 +0000712 so->weakreflist = NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +0000713
Raymond Hettingera38123e2003-11-24 22:18:49 +0000714 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 Hettingera690a992003-11-16 16:17:49 +0000723 return (PyObject *)so;
724}
725
726static PyObject *
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000727frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Raymond Hettingera690a992003-11-16 16:17:49 +0000728{
729 PyObject *iterable = NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000730 static PyObject *emptyfrozenset = NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +0000731
732 if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
733 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000734 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 Hettinger49ba4c32003-11-23 02:49:05 +0000743 Py_INCREF(iterable);
744 return iterable;
745 }
Raymond Hettingera690a992003-11-16 16:17:49 +0000746 return make_new_set(type, iterable);
747}
748
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000749static PyObject *
750set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
751{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000752 return make_new_set(type, NULL);
753}
754
Raymond Hettingera690a992003-11-16 16:17:49 +0000755static void
756set_dealloc(PySetObject *so)
757{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000758 register setentry *ep;
759 int fill = so->fill;
760
Raymond Hettingerbb999b52005-06-18 21:00:26 +0000761 PyObject_GC_UnTrack(so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000762 Py_TRASHCAN_SAFE_BEGIN(so)
Raymond Hettinger691d8052004-05-30 07:26:47 +0000763 if (so->weakreflist != NULL)
764 PyObject_ClearWeakRefs((PyObject *) so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000765
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 Hettingera690a992003-11-16 16:17:49 +0000775 so->ob_type->tp_free(so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000776 Py_TRASHCAN_SAFE_END(so)
Raymond Hettingera690a992003-11-16 16:17:49 +0000777}
778
Raymond Hettingerbb999b52005-06-18 21:00:26 +0000779static int
780set_traverse(PySetObject *so, visitproc visit, void *arg)
781{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000782 int i = 0;
783 PyObject *pk;
784
785 while (set_next_internal(so, &i, &pk))
786 Py_VISIT(pk);
Raymond Hettingerbb999b52005-06-18 21:00:26 +0000787 return 0;
788}
789
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000790static int
791set_len(PyObject *so)
Raymond Hettingera690a992003-11-16 16:17:49 +0000792{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000793 return ((PySetObject *)so)->used;
Raymond Hettingera690a992003-11-16 16:17:49 +0000794}
795
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000796static void
797set_swap_bodies(PySetObject *a, PySetObject *b)
Raymond Hettingera690a992003-11-16 16:17:49 +0000798{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000799 int t;
800 setentry *u;
801 setentry *(*f)(PySetObject *so, PyObject *key, long hash);
802 setentry tab[PySet_MINSIZE];
803 long h;
804
805 t = a->fill; a->fill = b->fill; b->fill = t;
806 t = a->used; a->used = b->used; b->used = t;
807 t = a->mask; a->mask = b->mask; b->mask = t;
808
809 u = a->table;
810 if (a->table == a->smalltable)
811 u = b->smalltable;
812 a->table = b->table;
813 if (b->table == b->smalltable)
814 a->table = a->smalltable;
815 b->table = u;
816
817 f = a->lookup; a->lookup = b->lookup; b->lookup = f;
818
819 if (a->table == a->smalltable || b->table == b->smalltable) {
820 memcpy(tab, a->smalltable, sizeof(tab));
821 memcpy(a->smalltable, b->smalltable, sizeof(tab));
822 memcpy(b->smalltable, tab, sizeof(tab));
823 }
824
825 h = a->hash; a->hash = b->hash; b->hash = h;
Raymond Hettingera690a992003-11-16 16:17:49 +0000826}
827
828static int
829set_contains(PySetObject *so, PyObject *key)
830{
Raymond Hettingerbfd334a2003-11-22 03:55:23 +0000831 PyObject *tmp;
Raymond Hettinger19c2d772003-11-21 18:36:54 +0000832 int result;
833
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000834 result = set_contains_internal(so, key);
Raymond Hettingera38123e2003-11-24 22:18:49 +0000835 if (result == -1 && PyAnySet_Check(key)) {
Raymond Hettinger19c2d772003-11-21 18:36:54 +0000836 PyErr_Clear();
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000837 tmp = make_new_set(&PyFrozenSet_Type, NULL);
Raymond Hettinger19c2d772003-11-21 18:36:54 +0000838 if (tmp == NULL)
839 return -1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000840 set_swap_bodies((PySetObject *)tmp, (PySetObject *)key);
841 result = set_contains_internal(so, tmp);
842 set_swap_bodies((PySetObject *)tmp, (PySetObject *)key);
Raymond Hettinger19c2d772003-11-21 18:36:54 +0000843 Py_DECREF(tmp);
844 }
845 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +0000846}
847
848static PyObject *
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +0000849set_direct_contains(PySetObject *so, PyObject *key)
850{
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +0000851 long result;
852
Raymond Hettinger438e02d2003-12-13 19:38:47 +0000853 result = set_contains(so, key);
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +0000854 if (result == -1)
855 return NULL;
856 return PyBool_FromLong(result);
857}
858
859PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
860
861static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +0000862set_copy(PySetObject *so)
863{
Raymond Hettingera38123e2003-11-24 22:18:49 +0000864 return make_new_set(so->ob_type, (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +0000865}
866
Raymond Hettinger49ba4c32003-11-23 02:49:05 +0000867static PyObject *
868frozenset_copy(PySetObject *so)
869{
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000870 if (PyFrozenSet_CheckExact(so)) {
Raymond Hettinger49ba4c32003-11-23 02:49:05 +0000871 Py_INCREF(so);
872 return (PyObject *)so;
873 }
874 return set_copy(so);
875}
876
Raymond Hettingera690a992003-11-16 16:17:49 +0000877PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
878
879static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000880set_union(PySetObject *so, PyObject *other)
881{
882 PySetObject *result;
883 PyObject *rv;
884
885 result = (PySetObject *)set_copy(so);
886 if (result == NULL)
887 return NULL;
Raymond Hettingera38123e2003-11-24 22:18:49 +0000888 rv = set_update(result, other);
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000889 if (rv == NULL) {
890 Py_DECREF(result);
891 return NULL;
892 }
893 Py_DECREF(rv);
894 return (PyObject *)result;
895}
896
897PyDoc_STRVAR(union_doc,
898 "Return the union of two sets as a new set.\n\
899\n\
900(i.e. all elements that are in either set.)");
901
902static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +0000903set_or(PySetObject *so, PyObject *other)
904{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000905 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000906 Py_INCREF(Py_NotImplemented);
907 return Py_NotImplemented;
908 }
909 return set_union(so, other);
910}
911
912static PyObject *
913set_ior(PySetObject *so, PyObject *other)
914{
915 PyObject *result;
916
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000917 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000918 Py_INCREF(Py_NotImplemented);
919 return Py_NotImplemented;
920 }
Raymond Hettingera38123e2003-11-24 22:18:49 +0000921 result = set_update(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +0000922 if (result == NULL)
923 return NULL;
924 Py_DECREF(result);
925 Py_INCREF(so);
926 return (PyObject *)so;
927}
928
929static PyObject *
930set_intersection(PySetObject *so, PyObject *other)
931{
932 PySetObject *result;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000933 PyObject *item, *it, *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +0000934
935 result = (PySetObject *)make_new_set(so->ob_type, NULL);
936 if (result == NULL)
937 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000938
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000939 if (PyAnySet_Check(other) && set_len(other) > set_len((PyObject *)so)) {
940 tmp = (PyObject *)so;
941 so = (PySetObject *)other;
Raymond Hettingera3b11e72003-12-31 14:08:58 +0000942 other = tmp;
943 }
944
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000945 if (PyAnySet_Check(other)) {
Raymond Hettingera3b11e72003-12-31 14:08:58 +0000946 int pos = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000947 while (set_next_internal((PySetObject *)other, &pos, &item)) {
948 if (set_contains_internal(so, item)) {
949 if (set_add_internal(result, item) == -1) {
Raymond Hettingera3b11e72003-12-31 14:08:58 +0000950 Py_DECREF(result);
951 return NULL;
952 }
953 }
954 }
955 return (PyObject *)result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000956 }
957
Raymond Hettingera690a992003-11-16 16:17:49 +0000958 it = PyObject_GetIter(other);
959 if (it == NULL) {
960 Py_DECREF(result);
961 return NULL;
962 }
963
Raymond Hettingera690a992003-11-16 16:17:49 +0000964 while ((item = PyIter_Next(it)) != NULL) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000965 if (set_contains_internal(so, item)) {
966 if (set_add_internal(result, item) == -1) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000967 Py_DECREF(it);
968 Py_DECREF(result);
969 Py_DECREF(item);
Raymond Hettingera690a992003-11-16 16:17:49 +0000970 return NULL;
971 }
972 }
973 Py_DECREF(item);
974 }
975 Py_DECREF(it);
976 if (PyErr_Occurred()) {
977 Py_DECREF(result);
978 return NULL;
979 }
980 return (PyObject *)result;
981}
982
983PyDoc_STRVAR(intersection_doc,
984"Return the intersection of two sets as a new set.\n\
985\n\
986(i.e. all elements that are in both sets.)");
987
988static PyObject *
989set_intersection_update(PySetObject *so, PyObject *other)
990{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000991 PyObject *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +0000992
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000993 tmp = set_intersection(so, other);
994 if (tmp == NULL)
Raymond Hettingera690a992003-11-16 16:17:49 +0000995 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000996 set_swap_bodies(so, (PySetObject *)tmp);
Raymond Hettingera690a992003-11-16 16:17:49 +0000997 Py_DECREF(tmp);
998 Py_RETURN_NONE;
999}
1000
1001PyDoc_STRVAR(intersection_update_doc,
1002"Update a set with the intersection of itself and another.");
1003
1004static PyObject *
1005set_and(PySetObject *so, PyObject *other)
1006{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001007 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001008 Py_INCREF(Py_NotImplemented);
1009 return Py_NotImplemented;
1010 }
1011 return set_intersection(so, other);
1012}
1013
1014static PyObject *
1015set_iand(PySetObject *so, PyObject *other)
1016{
1017 PyObject *result;
1018
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001019 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001020 Py_INCREF(Py_NotImplemented);
1021 return Py_NotImplemented;
1022 }
1023 result = set_intersection_update(so, other);
1024 if (result == NULL)
1025 return NULL;
1026 Py_DECREF(result);
1027 Py_INCREF(so);
1028 return (PyObject *)so;
1029}
1030
1031static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001032set_difference_update(PySetObject *so, PyObject *other)
1033{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001034 PyObject *item, *it;
Raymond Hettingera690a992003-11-16 16:17:49 +00001035
1036 it = PyObject_GetIter(other);
1037 if (it == NULL)
1038 return NULL;
1039
Raymond Hettingera690a992003-11-16 16:17:49 +00001040 while ((item = PyIter_Next(it)) != NULL) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001041 if (set_discard_internal(so, item) == -1) {
1042 Py_DECREF(it);
1043 Py_DECREF(item);
1044 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001045 }
1046 Py_DECREF(item);
1047 }
1048 Py_DECREF(it);
1049 if (PyErr_Occurred())
1050 return NULL;
1051 Py_RETURN_NONE;
1052}
1053
1054PyDoc_STRVAR(difference_update_doc,
1055"Remove all elements of another set from this set.");
1056
1057static PyObject *
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001058set_difference(PySetObject *so, PyObject *other)
1059{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001060 PyObject *tmp, *key, *result;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001061 int pos = 0;
1062
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001063 if (!PyAnySet_Check(other) && !PyDict_Check(other)) {
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001064 result = set_copy(so);
1065 if (result == NULL)
1066 return result;
1067 tmp = set_difference_update((PySetObject *)result, other);
1068 if (tmp != NULL) {
1069 Py_DECREF(tmp);
1070 return result;
1071 }
1072 Py_DECREF(result);
1073 return NULL;
1074 }
1075
1076 result = make_new_set(so->ob_type, NULL);
1077 if (result == NULL)
1078 return NULL;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001079
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001080 if (PyDict_Check(other)) {
1081 while (set_next_internal(so, &pos, &key)) {
1082 if (!PyDict_Contains(other, key)) {
1083 if (set_add_internal((PySetObject *)result, key) == -1)
1084 return NULL;
1085 }
1086 }
1087 return result;
1088 }
1089
1090 while (set_next_internal(so, &pos, &key)) {
1091 if (!set_contains_internal((PySetObject *)other, key)) {
1092 if (set_add_internal((PySetObject *)result, key) == -1)
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001093 return NULL;
1094 }
1095 }
1096 return result;
1097}
1098
1099PyDoc_STRVAR(difference_doc,
1100"Return the difference of two sets as a new set.\n\
1101\n\
1102(i.e. all elements that are in this set but not the other.)");
1103static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001104set_sub(PySetObject *so, PyObject *other)
1105{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001106 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001107 Py_INCREF(Py_NotImplemented);
1108 return Py_NotImplemented;
1109 }
1110 return set_difference(so, other);
1111}
1112
1113static PyObject *
1114set_isub(PySetObject *so, PyObject *other)
1115{
1116 PyObject *result;
1117
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001118 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001119 Py_INCREF(Py_NotImplemented);
1120 return Py_NotImplemented;
1121 }
1122 result = set_difference_update(so, other);
1123 if (result == NULL)
1124 return NULL;
1125 Py_DECREF(result);
1126 Py_INCREF(so);
1127 return (PyObject *)so;
1128}
1129
1130static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001131set_symmetric_difference_update(PySetObject *so, PyObject *other)
1132{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001133 PySetObject *otherset;
1134 PyObject *key;
Raymond Hettingerdc5ae112003-12-13 14:46:46 +00001135 int pos = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +00001136
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001137 if (PyDict_Check(other)) {
1138 PyObject *value;
1139 int rv;
1140 while (PyDict_Next(other, &pos, &key, &value)) {
1141 rv = set_discard_internal(so, key);
1142 if (rv == -1)
1143 return NULL;
1144 if (rv == DISCARD_NOTFOUND) {
1145 if (set_add_internal(so, key) == -1)
1146 return NULL;
1147 }
1148 }
1149 Py_RETURN_NONE;
1150 }
1151
1152 if (PyAnySet_Check(other)) {
1153 Py_INCREF(other);
1154 otherset = (PySetObject *)other;
1155 } else {
Raymond Hettingera690a992003-11-16 16:17:49 +00001156 otherset = (PySetObject *)make_new_set(so->ob_type, other);
1157 if (otherset == NULL)
1158 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001159 }
1160
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001161 while (set_next_internal(otherset, &pos, &key)) {
1162 int rv = set_discard_internal(so, key);
1163 if (rv == -1) {
1164 Py_XDECREF(otherset);
1165 return NULL;
1166 }
1167 if (rv == DISCARD_NOTFOUND) {
1168 if (set_add_internal(so, key) == -1) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001169 Py_XDECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001170 return NULL;
1171 }
1172 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001173 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001174 Py_DECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001175 Py_RETURN_NONE;
1176}
1177
1178PyDoc_STRVAR(symmetric_difference_update_doc,
1179"Update a set with the symmetric difference of itself and another.");
1180
1181static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001182set_symmetric_difference(PySetObject *so, PyObject *other)
1183{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001184 PyObject *rv;
1185 PySetObject *otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001186
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001187 otherset = (PySetObject *)make_new_set(so->ob_type, other);
1188 if (otherset == NULL)
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001189 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001190 rv = set_symmetric_difference_update(otherset, (PyObject *)so);
1191 if (rv == NULL)
1192 return NULL;
1193 Py_DECREF(rv);
1194 return (PyObject *)otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001195}
1196
1197PyDoc_STRVAR(symmetric_difference_doc,
1198"Return the symmetric difference of two sets as a new set.\n\
1199\n\
1200(i.e. all elements that are in exactly one of the sets.)");
1201
1202static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001203set_xor(PySetObject *so, PyObject *other)
1204{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001205 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001206 Py_INCREF(Py_NotImplemented);
1207 return Py_NotImplemented;
1208 }
1209 return set_symmetric_difference(so, other);
1210}
1211
1212static PyObject *
1213set_ixor(PySetObject *so, PyObject *other)
1214{
1215 PyObject *result;
1216
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001217 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001218 Py_INCREF(Py_NotImplemented);
1219 return Py_NotImplemented;
1220 }
1221 result = set_symmetric_difference_update(so, other);
1222 if (result == NULL)
1223 return NULL;
1224 Py_DECREF(result);
1225 Py_INCREF(so);
1226 return (PyObject *)so;
1227}
1228
1229static PyObject *
1230set_issubset(PySetObject *so, PyObject *other)
1231{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001232 PyObject *tmp, *result;
1233 PyObject *key;
Raymond Hettingerdc5ae112003-12-13 14:46:46 +00001234 int pos = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +00001235
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001236 if (!PyAnySet_Check(other)) {
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001237 tmp = make_new_set(&PySet_Type, other);
1238 if (tmp == NULL)
1239 return NULL;
1240 result = set_issubset(so, tmp);
1241 Py_DECREF(tmp);
1242 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001243 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001244 if (set_len((PyObject *)so) > set_len(other))
Raymond Hettingera690a992003-11-16 16:17:49 +00001245 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001246
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001247 while (set_next_internal(so, &pos, &key)) {
1248 if (!set_contains_internal((PySetObject *)other, key))
Raymond Hettingera690a992003-11-16 16:17:49 +00001249 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001250 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001251 Py_RETURN_TRUE;
1252}
1253
1254PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
1255
1256static PyObject *
1257set_issuperset(PySetObject *so, PyObject *other)
1258{
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001259 PyObject *tmp, *result;
1260
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001261 if (!PyAnySet_Check(other)) {
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001262 tmp = make_new_set(&PySet_Type, other);
1263 if (tmp == NULL)
1264 return NULL;
1265 result = set_issuperset(so, tmp);
1266 Py_DECREF(tmp);
1267 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001268 }
1269 return set_issubset((PySetObject *)other, (PyObject *)so);
1270}
1271
1272PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
1273
1274static long
1275set_nohash(PyObject *self)
1276{
1277 PyErr_SetString(PyExc_TypeError, "set objects are unhashable");
1278 return -1;
1279}
1280
1281static int
1282set_nocmp(PyObject *self)
1283{
1284 PyErr_SetString(PyExc_TypeError, "cannot compare sets using cmp()");
1285 return -1;
1286}
1287
1288static long
1289frozenset_hash(PyObject *self)
1290{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001291 PyObject *key;
Raymond Hettingera690a992003-11-16 16:17:49 +00001292 PySetObject *so = (PySetObject *)self;
Raymond Hettingerdc5ae112003-12-13 14:46:46 +00001293 int pos = 0;
Raymond Hettingerc9786332004-06-10 22:41:48 +00001294 long hash = 1927868237L;
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001295
Raymond Hettingera690a992003-11-16 16:17:49 +00001296 if (so->hash != -1)
1297 return so->hash;
Raymond Hettingera690a992003-11-16 16:17:49 +00001298
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001299 hash *= set_len(self) + 1;
1300 while (set_next_internal(so, &pos, &key)) {
Raymond Hettingerc9786332004-06-10 22:41:48 +00001301 /* Work to increase the bit dispersion for closely spaced hash
1302 values. The is important because some use cases have many
1303 combinations of a small number of elements with nearby
1304 hashes so that many distinct combinations collapse to only
1305 a handful of distinct hash values. */
1306 long h = PyObject_Hash(key);
1307 hash ^= (h ^ (h << 16) ^ 89869747L) * 3644798167u;
Raymond Hettingera690a992003-11-16 16:17:49 +00001308 }
Raymond Hettingerc9786332004-06-10 22:41:48 +00001309 hash = hash * 69069L + 907133923L;
Raymond Hettinger27e403e2004-06-10 21:38:41 +00001310 if (hash == -1)
1311 hash = 590923713L;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001312 so->hash = hash;
Raymond Hettingera690a992003-11-16 16:17:49 +00001313 return hash;
1314}
1315
1316static PyObject *
1317set_richcompare(PySetObject *v, PyObject *w, int op)
1318{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001319 PyObject *r1, *r2;
1320
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001321 if(!PyAnySet_Check(w)) {
1322 if (op == Py_EQ)
1323 Py_RETURN_FALSE;
1324 if (op == Py_NE)
1325 Py_RETURN_TRUE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001326 PyErr_SetString(PyExc_TypeError, "can only compare to a set");
1327 return NULL;
1328 }
1329 switch (op) {
1330 case Py_EQ:
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001331 if (set_len((PyObject *)v) != set_len(w))
Raymond Hettingera690a992003-11-16 16:17:49 +00001332 Py_RETURN_FALSE;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001333 return set_issubset(v, w);
1334 case Py_NE:
1335 if (set_len((PyObject *)v) != set_len(w))
1336 Py_RETURN_TRUE;
1337 r1 = set_issubset(v, w);
1338 assert (r1 != NULL);
1339 r2 = PyBool_FromLong(PyObject_Not(r1));
1340 Py_DECREF(r1);
1341 return r2;
1342 case Py_LE:
1343 return set_issubset(v, w);
1344 case Py_GE:
1345 return set_issuperset(v, w);
1346 case Py_LT:
1347 if (set_len((PyObject *)v) >= set_len(w))
1348 Py_RETURN_FALSE;
1349 return set_issubset(v, w);
1350 case Py_GT:
1351 if (set_len((PyObject *)v) <= set_len(w))
1352 Py_RETURN_FALSE;
1353 return set_issuperset(v, w);
Raymond Hettingera690a992003-11-16 16:17:49 +00001354 }
1355 Py_INCREF(Py_NotImplemented);
1356 return Py_NotImplemented;
1357}
1358
1359static PyObject *
1360set_repr(PySetObject *so)
1361{
1362 PyObject *keys, *result, *listrepr;
1363
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001364 keys = PySequence_List((PyObject *)so);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001365 if (keys == NULL)
1366 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001367 listrepr = PyObject_Repr(keys);
1368 Py_DECREF(keys);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001369 if (listrepr == NULL)
1370 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001371
1372 result = PyString_FromFormat("%s(%s)", so->ob_type->tp_name,
1373 PyString_AS_STRING(listrepr));
1374 Py_DECREF(listrepr);
1375 return result;
1376}
1377
1378static int
1379set_tp_print(PySetObject *so, FILE *fp, int flags)
1380{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001381 PyObject *key;
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001382 int pos=0;
1383 char *emit = ""; /* No separator emitted on first pass */
1384 char *separator = ", ";
Raymond Hettingera690a992003-11-16 16:17:49 +00001385
Raymond Hettingera690a992003-11-16 16:17:49 +00001386 fprintf(fp, "%s([", so->ob_type->tp_name);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001387 while (set_next_internal(so, &pos, &key)) {
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001388 fputs(emit, fp);
1389 emit = separator;
Raymond Hettingerdc5ae112003-12-13 14:46:46 +00001390 if (PyObject_Print(key, fp, 0) != 0)
Raymond Hettingera690a992003-11-16 16:17:49 +00001391 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +00001392 }
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001393 fputs("])", fp);
Raymond Hettingera690a992003-11-16 16:17:49 +00001394 return 0;
1395}
1396
1397static PyObject *
1398set_clear(PySetObject *so)
1399{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001400 set_clear_internal(so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001401 so->hash = -1;
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001402 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001403}
1404
1405PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
1406
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001407static int
1408set_tp_clear(PySetObject *so)
1409{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001410 set_clear_internal(so);
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001411 so->hash = -1;
1412 return 0;
1413}
1414
Raymond Hettingera690a992003-11-16 16:17:49 +00001415static PyObject *
1416set_add(PySetObject *so, PyObject *item)
1417{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001418 if (set_add_internal(so, item) == -1)
Raymond Hettingera690a992003-11-16 16:17:49 +00001419 return NULL;
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001420 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001421}
1422
1423PyDoc_STRVAR(add_doc,
1424"Add an element to a set.\n\
1425\n\
1426This has no effect if the element is already present.");
1427
1428static PyObject *
1429set_remove(PySetObject *so, PyObject *item)
1430{
Raymond Hettinger0deab622003-12-13 18:53:18 +00001431 PyObject *tmp, *result;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001432 int rv;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001433
Raymond Hettinger0deab622003-12-13 18:53:18 +00001434 if (PyType_IsSubtype(item->ob_type, &PySet_Type)) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001435 tmp = make_new_set(&PyFrozenSet_Type, NULL);
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001436 if (tmp == NULL)
1437 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001438 set_swap_bodies((PySetObject *)item, (PySetObject *)tmp);
Raymond Hettinger0deab622003-12-13 18:53:18 +00001439 result = set_remove(so, tmp);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001440 set_swap_bodies((PySetObject *)item, (PySetObject *)tmp);
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001441 Py_DECREF(tmp);
Raymond Hettinger0deab622003-12-13 18:53:18 +00001442 return result;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001443 }
Raymond Hettinger0deab622003-12-13 18:53:18 +00001444
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001445 rv = set_discard_internal(so, item);
1446 if (rv == -1)
Raymond Hettinger0deab622003-12-13 18:53:18 +00001447 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001448 else if (rv == DISCARD_NOTFOUND) {
1449 PyErr_SetObject(PyExc_KeyError, item);
1450 return NULL;
1451 }
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001452 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001453}
1454
1455PyDoc_STRVAR(remove_doc,
1456"Remove an element from a set; it must be a member.\n\
1457\n\
1458If the element is not a member, raise a KeyError.");
1459
1460static PyObject *
1461set_discard(PySetObject *so, PyObject *item)
1462{
Raymond Hettinger0deab622003-12-13 18:53:18 +00001463 PyObject *tmp, *result;
1464
1465 if (PyType_IsSubtype(item->ob_type, &PySet_Type)) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001466 tmp = make_new_set(&PyFrozenSet_Type, NULL);
Raymond Hettinger0deab622003-12-13 18:53:18 +00001467 if (tmp == NULL)
1468 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001469 set_swap_bodies((PySetObject *)item, (PySetObject *)tmp);
Raymond Hettinger0deab622003-12-13 18:53:18 +00001470 result = set_discard(so, tmp);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001471 set_swap_bodies((PySetObject *)item, (PySetObject *)tmp);
Raymond Hettinger0deab622003-12-13 18:53:18 +00001472 Py_DECREF(tmp);
1473 return result;
1474 }
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001475
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001476 if (set_discard_internal(so, item) == -1)
1477 return NULL;
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001478 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001479}
1480
1481PyDoc_STRVAR(discard_doc,
1482"Remove an element from a set if it is a member.\n\
1483\n\
1484If the element is not a member, do nothing.");
1485
1486static PyObject *
1487set_pop(PySetObject *so)
1488{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001489 PyObject *key;
Raymond Hettingera690a992003-11-16 16:17:49 +00001490 int pos = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001491 int rv;
Raymond Hettingera690a992003-11-16 16:17:49 +00001492
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001493 if (!set_next_internal(so, &pos, &key)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001494 PyErr_SetString(PyExc_KeyError, "pop from an empty set");
1495 return NULL;
1496 }
1497 Py_INCREF(key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001498
1499 rv = set_discard_internal(so, key);
1500 if (rv == -1) {
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001501 Py_DECREF(key);
1502 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001503 } else if (rv == DISCARD_NOTFOUND) {
1504 Py_DECREF(key);
1505 PyErr_SetObject(PyExc_KeyError, key);
1506 return NULL;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001507 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001508 return key;
1509}
1510
1511PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.");
1512
1513static PyObject *
1514set_reduce(PySetObject *so)
1515{
Raymond Hettinger15056a52004-11-09 07:25:31 +00001516 PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001517
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001518 keys = PySequence_List((PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001519 if (keys == NULL)
1520 goto done;
1521 args = PyTuple_Pack(1, keys);
1522 if (args == NULL)
1523 goto done;
Raymond Hettinger15056a52004-11-09 07:25:31 +00001524 dict = PyObject_GetAttrString((PyObject *)so, "__dict__");
1525 if (dict == NULL) {
1526 PyErr_Clear();
1527 dict = Py_None;
1528 Py_INCREF(dict);
1529 }
1530 result = PyTuple_Pack(3, so->ob_type, args, dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001531done:
1532 Py_XDECREF(args);
1533 Py_XDECREF(keys);
Raymond Hettinger15056a52004-11-09 07:25:31 +00001534 Py_XDECREF(dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001535 return result;
1536}
1537
1538PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
1539
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001540static int
1541set_init(PySetObject *self, PyObject *args, PyObject *kwds)
1542{
1543 PyObject *iterable = NULL;
1544 PyObject *result;
1545
1546 if (!PyAnySet_Check(self))
1547 return -1;
1548 if (!PyArg_UnpackTuple(args, self->ob_type->tp_name, 0, 1, &iterable))
1549 return -1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001550 set_clear_internal(self);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001551 self->hash = -1;
1552 if (iterable == NULL)
1553 return 0;
Raymond Hettingera38123e2003-11-24 22:18:49 +00001554 result = set_update(self, iterable);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001555 if (result != NULL) {
1556 Py_DECREF(result);
1557 return 0;
1558 }
1559 return -1;
1560}
1561
Raymond Hettingera690a992003-11-16 16:17:49 +00001562static PySequenceMethods set_as_sequence = {
1563 (inquiry)set_len, /* sq_length */
1564 0, /* sq_concat */
1565 0, /* sq_repeat */
1566 0, /* sq_item */
1567 0, /* sq_slice */
1568 0, /* sq_ass_item */
1569 0, /* sq_ass_slice */
1570 (objobjproc)set_contains, /* sq_contains */
1571};
1572
1573/* set object ********************************************************/
1574
1575static PyMethodDef set_methods[] = {
1576 {"add", (PyCFunction)set_add, METH_O,
1577 add_doc},
1578 {"clear", (PyCFunction)set_clear, METH_NOARGS,
1579 clear_doc},
Raymond Hettinger0deab622003-12-13 18:53:18 +00001580 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001581 contains_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001582 {"copy", (PyCFunction)set_copy, METH_NOARGS,
1583 copy_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001584 {"discard", (PyCFunction)set_discard, METH_O,
1585 discard_doc},
1586 {"difference", (PyCFunction)set_difference, METH_O,
1587 difference_doc},
1588 {"difference_update", (PyCFunction)set_difference_update, METH_O,
1589 difference_update_doc},
1590 {"intersection",(PyCFunction)set_intersection, METH_O,
1591 intersection_doc},
1592 {"intersection_update",(PyCFunction)set_intersection_update, METH_O,
1593 intersection_update_doc},
1594 {"issubset", (PyCFunction)set_issubset, METH_O,
1595 issubset_doc},
1596 {"issuperset", (PyCFunction)set_issuperset, METH_O,
1597 issuperset_doc},
1598 {"pop", (PyCFunction)set_pop, METH_NOARGS,
1599 pop_doc},
1600 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
1601 reduce_doc},
1602 {"remove", (PyCFunction)set_remove, METH_O,
1603 remove_doc},
1604 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
1605 symmetric_difference_doc},
1606 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
1607 symmetric_difference_update_doc},
1608 {"union", (PyCFunction)set_union, METH_O,
1609 union_doc},
Raymond Hettingera38123e2003-11-24 22:18:49 +00001610 {"update", (PyCFunction)set_update, METH_O,
1611 update_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001612 {NULL, NULL} /* sentinel */
1613};
1614
1615static PyNumberMethods set_as_number = {
1616 0, /*nb_add*/
1617 (binaryfunc)set_sub, /*nb_subtract*/
1618 0, /*nb_multiply*/
1619 0, /*nb_divide*/
1620 0, /*nb_remainder*/
1621 0, /*nb_divmod*/
1622 0, /*nb_power*/
1623 0, /*nb_negative*/
1624 0, /*nb_positive*/
1625 0, /*nb_absolute*/
1626 0, /*nb_nonzero*/
1627 0, /*nb_invert*/
1628 0, /*nb_lshift*/
1629 0, /*nb_rshift*/
1630 (binaryfunc)set_and, /*nb_and*/
1631 (binaryfunc)set_xor, /*nb_xor*/
1632 (binaryfunc)set_or, /*nb_or*/
1633 0, /*nb_coerce*/
1634 0, /*nb_int*/
1635 0, /*nb_long*/
1636 0, /*nb_float*/
1637 0, /*nb_oct*/
1638 0, /*nb_hex*/
1639 0, /*nb_inplace_add*/
1640 (binaryfunc)set_isub, /*nb_inplace_subtract*/
1641 0, /*nb_inplace_multiply*/
1642 0, /*nb_inplace_divide*/
1643 0, /*nb_inplace_remainder*/
1644 0, /*nb_inplace_power*/
1645 0, /*nb_inplace_lshift*/
1646 0, /*nb_inplace_rshift*/
1647 (binaryfunc)set_iand, /*nb_inplace_and*/
1648 (binaryfunc)set_ixor, /*nb_inplace_xor*/
1649 (binaryfunc)set_ior, /*nb_inplace_or*/
1650};
1651
1652PyDoc_STRVAR(set_doc,
1653"set(iterable) --> set object\n\
1654\n\
1655Build an unordered collection.");
1656
1657PyTypeObject PySet_Type = {
1658 PyObject_HEAD_INIT(&PyType_Type)
1659 0, /* ob_size */
1660 "set", /* tp_name */
1661 sizeof(PySetObject), /* tp_basicsize */
1662 0, /* tp_itemsize */
1663 /* methods */
1664 (destructor)set_dealloc, /* tp_dealloc */
1665 (printfunc)set_tp_print, /* tp_print */
1666 0, /* tp_getattr */
1667 0, /* tp_setattr */
1668 (cmpfunc)set_nocmp, /* tp_compare */
1669 (reprfunc)set_repr, /* tp_repr */
1670 &set_as_number, /* tp_as_number */
1671 &set_as_sequence, /* tp_as_sequence */
1672 0, /* tp_as_mapping */
1673 set_nohash, /* tp_hash */
1674 0, /* tp_call */
1675 0, /* tp_str */
1676 PyObject_GenericGetAttr, /* tp_getattro */
1677 0, /* tp_setattro */
1678 0, /* tp_as_buffer */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001679 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES |
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001680 Py_TPFLAGS_BASETYPE, /* tp_flags */
Raymond Hettingera690a992003-11-16 16:17:49 +00001681 set_doc, /* tp_doc */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001682 (traverseproc)set_traverse, /* tp_traverse */
1683 (inquiry)set_tp_clear, /* tp_clear */
Raymond Hettingera690a992003-11-16 16:17:49 +00001684 (richcmpfunc)set_richcompare, /* tp_richcompare */
Raymond Hettinger691d8052004-05-30 07:26:47 +00001685 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001686 (getiterfunc)set_iter, /* tp_iter */
Raymond Hettingera690a992003-11-16 16:17:49 +00001687 0, /* tp_iternext */
1688 set_methods, /* tp_methods */
1689 0, /* tp_members */
1690 0, /* tp_getset */
1691 0, /* tp_base */
1692 0, /* tp_dict */
1693 0, /* tp_descr_get */
1694 0, /* tp_descr_set */
1695 0, /* tp_dictoffset */
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001696 (initproc)set_init, /* tp_init */
Raymond Hettingera690a992003-11-16 16:17:49 +00001697 PyType_GenericAlloc, /* tp_alloc */
1698 set_new, /* tp_new */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001699 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00001700};
1701
1702/* frozenset object ********************************************************/
1703
1704
1705static PyMethodDef frozenset_methods[] = {
Raymond Hettinger0deab622003-12-13 18:53:18 +00001706 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001707 contains_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001708 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
Raymond Hettingera690a992003-11-16 16:17:49 +00001709 copy_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001710 {"difference", (PyCFunction)set_difference, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00001711 difference_doc},
1712 {"intersection",(PyCFunction)set_intersection, METH_O,
1713 intersection_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001714 {"issubset", (PyCFunction)set_issubset, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00001715 issubset_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001716 {"issuperset", (PyCFunction)set_issuperset, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00001717 issuperset_doc},
1718 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
1719 reduce_doc},
1720 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
1721 symmetric_difference_doc},
1722 {"union", (PyCFunction)set_union, METH_O,
1723 union_doc},
1724 {NULL, NULL} /* sentinel */
1725};
1726
1727static PyNumberMethods frozenset_as_number = {
1728 0, /*nb_add*/
1729 (binaryfunc)set_sub, /*nb_subtract*/
1730 0, /*nb_multiply*/
1731 0, /*nb_divide*/
1732 0, /*nb_remainder*/
1733 0, /*nb_divmod*/
1734 0, /*nb_power*/
1735 0, /*nb_negative*/
1736 0, /*nb_positive*/
1737 0, /*nb_absolute*/
1738 0, /*nb_nonzero*/
1739 0, /*nb_invert*/
1740 0, /*nb_lshift*/
1741 0, /*nb_rshift*/
1742 (binaryfunc)set_and, /*nb_and*/
1743 (binaryfunc)set_xor, /*nb_xor*/
1744 (binaryfunc)set_or, /*nb_or*/
1745};
1746
1747PyDoc_STRVAR(frozenset_doc,
1748"frozenset(iterable) --> frozenset object\n\
1749\n\
1750Build an immutable unordered collection.");
1751
1752PyTypeObject PyFrozenSet_Type = {
1753 PyObject_HEAD_INIT(&PyType_Type)
1754 0, /* ob_size */
1755 "frozenset", /* tp_name */
1756 sizeof(PySetObject), /* tp_basicsize */
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001757 0, /* tp_itemsize */
1758 /* methods */
Raymond Hettingera690a992003-11-16 16:17:49 +00001759 (destructor)set_dealloc, /* tp_dealloc */
1760 (printfunc)set_tp_print, /* tp_print */
1761 0, /* tp_getattr */
1762 0, /* tp_setattr */
1763 (cmpfunc)set_nocmp, /* tp_compare */
1764 (reprfunc)set_repr, /* tp_repr */
1765 &frozenset_as_number, /* tp_as_number */
1766 &set_as_sequence, /* tp_as_sequence */
1767 0, /* tp_as_mapping */
1768 frozenset_hash, /* tp_hash */
1769 0, /* tp_call */
1770 0, /* tp_str */
1771 PyObject_GenericGetAttr, /* tp_getattro */
1772 0, /* tp_setattro */
1773 0, /* tp_as_buffer */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001774 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES |
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001775 Py_TPFLAGS_BASETYPE, /* tp_flags */
Raymond Hettingera690a992003-11-16 16:17:49 +00001776 frozenset_doc, /* tp_doc */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001777 (traverseproc)set_traverse, /* tp_traverse */
Raymond Hettingera690a992003-11-16 16:17:49 +00001778 0, /* tp_clear */
1779 (richcmpfunc)set_richcompare, /* tp_richcompare */
Raymond Hettinger691d8052004-05-30 07:26:47 +00001780 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
Raymond Hettingera690a992003-11-16 16:17:49 +00001781 (getiterfunc)set_iter, /* tp_iter */
1782 0, /* tp_iternext */
1783 frozenset_methods, /* tp_methods */
1784 0, /* tp_members */
1785 0, /* tp_getset */
1786 0, /* tp_base */
1787 0, /* tp_dict */
1788 0, /* tp_descr_get */
1789 0, /* tp_descr_set */
1790 0, /* tp_dictoffset */
1791 0, /* tp_init */
1792 PyType_GenericAlloc, /* tp_alloc */
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001793 frozenset_new, /* tp_new */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001794 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00001795};