blob: a279ec2a5ced7976352f10c9c21c80de9ee646f7 [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 Hettinger934d63e2005-07-31 01:33:10 +0000796/* set_swap_bodies() switches the contents of any two sets by moving their
797 internal data pointers and, if needed, copying the internal smalltables.
798 Semantically equivalent to:
799
800 t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t
801
802 The function always succeeds and it leaves both objects in a stable state.
803 Useful for creating temporary frozensets from sets for membership testing
804 in __contains__(), discard(), and remove(). Also useful for operations
805 that update in-place (by allowing an intermediate result to be swapped
806 into one of original the inputs).
807*/
808
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000809static void
810set_swap_bodies(PySetObject *a, PySetObject *b)
Raymond Hettingera690a992003-11-16 16:17:49 +0000811{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000812 int t;
813 setentry *u;
814 setentry *(*f)(PySetObject *so, PyObject *key, long hash);
815 setentry tab[PySet_MINSIZE];
816 long h;
817
818 t = a->fill; a->fill = b->fill; b->fill = t;
819 t = a->used; a->used = b->used; b->used = t;
820 t = a->mask; a->mask = b->mask; b->mask = t;
821
822 u = a->table;
823 if (a->table == a->smalltable)
824 u = b->smalltable;
825 a->table = b->table;
826 if (b->table == b->smalltable)
827 a->table = a->smalltable;
828 b->table = u;
829
830 f = a->lookup; a->lookup = b->lookup; b->lookup = f;
831
832 if (a->table == a->smalltable || b->table == b->smalltable) {
833 memcpy(tab, a->smalltable, sizeof(tab));
834 memcpy(a->smalltable, b->smalltable, sizeof(tab));
835 memcpy(b->smalltable, tab, sizeof(tab));
836 }
837
838 h = a->hash; a->hash = b->hash; b->hash = h;
Raymond Hettingera690a992003-11-16 16:17:49 +0000839}
840
841static int
842set_contains(PySetObject *so, PyObject *key)
843{
Raymond Hettingerbfd334a2003-11-22 03:55:23 +0000844 PyObject *tmp;
Raymond Hettinger19c2d772003-11-21 18:36:54 +0000845 int result;
846
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000847 result = set_contains_internal(so, key);
Raymond Hettingera38123e2003-11-24 22:18:49 +0000848 if (result == -1 && PyAnySet_Check(key)) {
Raymond Hettinger19c2d772003-11-21 18:36:54 +0000849 PyErr_Clear();
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000850 tmp = make_new_set(&PyFrozenSet_Type, NULL);
Raymond Hettinger19c2d772003-11-21 18:36:54 +0000851 if (tmp == NULL)
852 return -1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000853 set_swap_bodies((PySetObject *)tmp, (PySetObject *)key);
854 result = set_contains_internal(so, tmp);
855 set_swap_bodies((PySetObject *)tmp, (PySetObject *)key);
Raymond Hettinger19c2d772003-11-21 18:36:54 +0000856 Py_DECREF(tmp);
857 }
858 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +0000859}
860
861static PyObject *
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +0000862set_direct_contains(PySetObject *so, PyObject *key)
863{
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +0000864 long result;
865
Raymond Hettinger438e02d2003-12-13 19:38:47 +0000866 result = set_contains(so, key);
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +0000867 if (result == -1)
868 return NULL;
869 return PyBool_FromLong(result);
870}
871
872PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
873
874static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +0000875set_copy(PySetObject *so)
876{
Raymond Hettingera38123e2003-11-24 22:18:49 +0000877 return make_new_set(so->ob_type, (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +0000878}
879
Raymond Hettinger49ba4c32003-11-23 02:49:05 +0000880static PyObject *
881frozenset_copy(PySetObject *so)
882{
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000883 if (PyFrozenSet_CheckExact(so)) {
Raymond Hettinger49ba4c32003-11-23 02:49:05 +0000884 Py_INCREF(so);
885 return (PyObject *)so;
886 }
887 return set_copy(so);
888}
889
Raymond Hettingera690a992003-11-16 16:17:49 +0000890PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
891
892static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000893set_union(PySetObject *so, PyObject *other)
894{
895 PySetObject *result;
896 PyObject *rv;
897
898 result = (PySetObject *)set_copy(so);
899 if (result == NULL)
900 return NULL;
Raymond Hettingera38123e2003-11-24 22:18:49 +0000901 rv = set_update(result, other);
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000902 if (rv == NULL) {
903 Py_DECREF(result);
904 return NULL;
905 }
906 Py_DECREF(rv);
907 return (PyObject *)result;
908}
909
910PyDoc_STRVAR(union_doc,
911 "Return the union of two sets as a new set.\n\
912\n\
913(i.e. all elements that are in either set.)");
914
915static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +0000916set_or(PySetObject *so, PyObject *other)
917{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000918 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000919 Py_INCREF(Py_NotImplemented);
920 return Py_NotImplemented;
921 }
922 return set_union(so, other);
923}
924
925static PyObject *
926set_ior(PySetObject *so, PyObject *other)
927{
928 PyObject *result;
929
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000930 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000931 Py_INCREF(Py_NotImplemented);
932 return Py_NotImplemented;
933 }
Raymond Hettingera38123e2003-11-24 22:18:49 +0000934 result = set_update(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +0000935 if (result == NULL)
936 return NULL;
937 Py_DECREF(result);
938 Py_INCREF(so);
939 return (PyObject *)so;
940}
941
942static PyObject *
943set_intersection(PySetObject *so, PyObject *other)
944{
945 PySetObject *result;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000946 PyObject *item, *it, *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +0000947
948 result = (PySetObject *)make_new_set(so->ob_type, NULL);
949 if (result == NULL)
950 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000951
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000952 if (PyAnySet_Check(other) && set_len(other) > set_len((PyObject *)so)) {
953 tmp = (PyObject *)so;
954 so = (PySetObject *)other;
Raymond Hettingera3b11e72003-12-31 14:08:58 +0000955 other = tmp;
956 }
957
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000958 if (PyAnySet_Check(other)) {
Raymond Hettingera3b11e72003-12-31 14:08:58 +0000959 int pos = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000960 while (set_next_internal((PySetObject *)other, &pos, &item)) {
961 if (set_contains_internal(so, item)) {
962 if (set_add_internal(result, item) == -1) {
Raymond Hettingera3b11e72003-12-31 14:08:58 +0000963 Py_DECREF(result);
964 return NULL;
965 }
966 }
967 }
968 return (PyObject *)result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000969 }
970
Raymond Hettingera690a992003-11-16 16:17:49 +0000971 it = PyObject_GetIter(other);
972 if (it == NULL) {
973 Py_DECREF(result);
974 return NULL;
975 }
976
Raymond Hettingera690a992003-11-16 16:17:49 +0000977 while ((item = PyIter_Next(it)) != NULL) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000978 if (set_contains_internal(so, item)) {
979 if (set_add_internal(result, item) == -1) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000980 Py_DECREF(it);
981 Py_DECREF(result);
982 Py_DECREF(item);
Raymond Hettingera690a992003-11-16 16:17:49 +0000983 return NULL;
984 }
985 }
986 Py_DECREF(item);
987 }
988 Py_DECREF(it);
989 if (PyErr_Occurred()) {
990 Py_DECREF(result);
991 return NULL;
992 }
993 return (PyObject *)result;
994}
995
996PyDoc_STRVAR(intersection_doc,
997"Return the intersection of two sets as a new set.\n\
998\n\
999(i.e. all elements that are in both sets.)");
1000
1001static PyObject *
1002set_intersection_update(PySetObject *so, PyObject *other)
1003{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001004 PyObject *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001005
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001006 tmp = set_intersection(so, other);
1007 if (tmp == NULL)
Raymond Hettingera690a992003-11-16 16:17:49 +00001008 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001009 set_swap_bodies(so, (PySetObject *)tmp);
Raymond Hettingera690a992003-11-16 16:17:49 +00001010 Py_DECREF(tmp);
1011 Py_RETURN_NONE;
1012}
1013
1014PyDoc_STRVAR(intersection_update_doc,
1015"Update a set with the intersection of itself and another.");
1016
1017static PyObject *
1018set_and(PySetObject *so, PyObject *other)
1019{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001020 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001021 Py_INCREF(Py_NotImplemented);
1022 return Py_NotImplemented;
1023 }
1024 return set_intersection(so, other);
1025}
1026
1027static PyObject *
1028set_iand(PySetObject *so, PyObject *other)
1029{
1030 PyObject *result;
1031
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001032 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001033 Py_INCREF(Py_NotImplemented);
1034 return Py_NotImplemented;
1035 }
1036 result = set_intersection_update(so, other);
1037 if (result == NULL)
1038 return NULL;
1039 Py_DECREF(result);
1040 Py_INCREF(so);
1041 return (PyObject *)so;
1042}
1043
1044static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001045set_difference_update(PySetObject *so, PyObject *other)
1046{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001047 PyObject *item, *it;
Raymond Hettingera690a992003-11-16 16:17:49 +00001048
1049 it = PyObject_GetIter(other);
1050 if (it == NULL)
1051 return NULL;
1052
Raymond Hettingera690a992003-11-16 16:17:49 +00001053 while ((item = PyIter_Next(it)) != NULL) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001054 if (set_discard_internal(so, item) == -1) {
1055 Py_DECREF(it);
1056 Py_DECREF(item);
1057 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001058 }
1059 Py_DECREF(item);
1060 }
1061 Py_DECREF(it);
1062 if (PyErr_Occurred())
1063 return NULL;
1064 Py_RETURN_NONE;
1065}
1066
1067PyDoc_STRVAR(difference_update_doc,
1068"Remove all elements of another set from this set.");
1069
1070static PyObject *
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001071set_difference(PySetObject *so, PyObject *other)
1072{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001073 PyObject *tmp, *key, *result;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001074 int pos = 0;
1075
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001076 if (!PyAnySet_Check(other) && !PyDict_Check(other)) {
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001077 result = set_copy(so);
1078 if (result == NULL)
1079 return result;
1080 tmp = set_difference_update((PySetObject *)result, other);
1081 if (tmp != NULL) {
1082 Py_DECREF(tmp);
1083 return result;
1084 }
1085 Py_DECREF(result);
1086 return NULL;
1087 }
1088
1089 result = make_new_set(so->ob_type, NULL);
1090 if (result == NULL)
1091 return NULL;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001092
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001093 if (PyDict_Check(other)) {
1094 while (set_next_internal(so, &pos, &key)) {
1095 if (!PyDict_Contains(other, key)) {
1096 if (set_add_internal((PySetObject *)result, key) == -1)
1097 return NULL;
1098 }
1099 }
1100 return result;
1101 }
1102
1103 while (set_next_internal(so, &pos, &key)) {
1104 if (!set_contains_internal((PySetObject *)other, key)) {
1105 if (set_add_internal((PySetObject *)result, key) == -1)
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001106 return NULL;
1107 }
1108 }
1109 return result;
1110}
1111
1112PyDoc_STRVAR(difference_doc,
1113"Return the difference of two sets as a new set.\n\
1114\n\
1115(i.e. all elements that are in this set but not the other.)");
1116static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001117set_sub(PySetObject *so, PyObject *other)
1118{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001119 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001120 Py_INCREF(Py_NotImplemented);
1121 return Py_NotImplemented;
1122 }
1123 return set_difference(so, other);
1124}
1125
1126static PyObject *
1127set_isub(PySetObject *so, PyObject *other)
1128{
1129 PyObject *result;
1130
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001131 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001132 Py_INCREF(Py_NotImplemented);
1133 return Py_NotImplemented;
1134 }
1135 result = set_difference_update(so, other);
1136 if (result == NULL)
1137 return NULL;
1138 Py_DECREF(result);
1139 Py_INCREF(so);
1140 return (PyObject *)so;
1141}
1142
1143static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001144set_symmetric_difference_update(PySetObject *so, PyObject *other)
1145{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001146 PySetObject *otherset;
1147 PyObject *key;
Raymond Hettingerdc5ae112003-12-13 14:46:46 +00001148 int pos = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +00001149
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001150 if (PyDict_Check(other)) {
1151 PyObject *value;
1152 int rv;
1153 while (PyDict_Next(other, &pos, &key, &value)) {
1154 rv = set_discard_internal(so, key);
1155 if (rv == -1)
1156 return NULL;
1157 if (rv == DISCARD_NOTFOUND) {
1158 if (set_add_internal(so, key) == -1)
1159 return NULL;
1160 }
1161 }
1162 Py_RETURN_NONE;
1163 }
1164
1165 if (PyAnySet_Check(other)) {
1166 Py_INCREF(other);
1167 otherset = (PySetObject *)other;
1168 } else {
Raymond Hettingera690a992003-11-16 16:17:49 +00001169 otherset = (PySetObject *)make_new_set(so->ob_type, other);
1170 if (otherset == NULL)
1171 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001172 }
1173
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001174 while (set_next_internal(otherset, &pos, &key)) {
1175 int rv = set_discard_internal(so, key);
1176 if (rv == -1) {
1177 Py_XDECREF(otherset);
1178 return NULL;
1179 }
1180 if (rv == DISCARD_NOTFOUND) {
1181 if (set_add_internal(so, key) == -1) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001182 Py_XDECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001183 return NULL;
1184 }
1185 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001186 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001187 Py_DECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001188 Py_RETURN_NONE;
1189}
1190
1191PyDoc_STRVAR(symmetric_difference_update_doc,
1192"Update a set with the symmetric difference of itself and another.");
1193
1194static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001195set_symmetric_difference(PySetObject *so, PyObject *other)
1196{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001197 PyObject *rv;
1198 PySetObject *otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001199
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001200 otherset = (PySetObject *)make_new_set(so->ob_type, other);
1201 if (otherset == NULL)
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001202 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001203 rv = set_symmetric_difference_update(otherset, (PyObject *)so);
1204 if (rv == NULL)
1205 return NULL;
1206 Py_DECREF(rv);
1207 return (PyObject *)otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001208}
1209
1210PyDoc_STRVAR(symmetric_difference_doc,
1211"Return the symmetric difference of two sets as a new set.\n\
1212\n\
1213(i.e. all elements that are in exactly one of the sets.)");
1214
1215static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001216set_xor(PySetObject *so, PyObject *other)
1217{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001218 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001219 Py_INCREF(Py_NotImplemented);
1220 return Py_NotImplemented;
1221 }
1222 return set_symmetric_difference(so, other);
1223}
1224
1225static PyObject *
1226set_ixor(PySetObject *so, PyObject *other)
1227{
1228 PyObject *result;
1229
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001230 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001231 Py_INCREF(Py_NotImplemented);
1232 return Py_NotImplemented;
1233 }
1234 result = set_symmetric_difference_update(so, other);
1235 if (result == NULL)
1236 return NULL;
1237 Py_DECREF(result);
1238 Py_INCREF(so);
1239 return (PyObject *)so;
1240}
1241
1242static PyObject *
1243set_issubset(PySetObject *so, PyObject *other)
1244{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001245 PyObject *tmp, *result;
1246 PyObject *key;
Raymond Hettingerdc5ae112003-12-13 14:46:46 +00001247 int pos = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +00001248
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001249 if (!PyAnySet_Check(other)) {
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001250 tmp = make_new_set(&PySet_Type, other);
1251 if (tmp == NULL)
1252 return NULL;
1253 result = set_issubset(so, tmp);
1254 Py_DECREF(tmp);
1255 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001256 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001257 if (set_len((PyObject *)so) > set_len(other))
Raymond Hettingera690a992003-11-16 16:17:49 +00001258 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001259
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001260 while (set_next_internal(so, &pos, &key)) {
1261 if (!set_contains_internal((PySetObject *)other, key))
Raymond Hettingera690a992003-11-16 16:17:49 +00001262 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001263 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001264 Py_RETURN_TRUE;
1265}
1266
1267PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
1268
1269static PyObject *
1270set_issuperset(PySetObject *so, PyObject *other)
1271{
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001272 PyObject *tmp, *result;
1273
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001274 if (!PyAnySet_Check(other)) {
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001275 tmp = make_new_set(&PySet_Type, other);
1276 if (tmp == NULL)
1277 return NULL;
1278 result = set_issuperset(so, tmp);
1279 Py_DECREF(tmp);
1280 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001281 }
1282 return set_issubset((PySetObject *)other, (PyObject *)so);
1283}
1284
1285PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
1286
1287static long
1288set_nohash(PyObject *self)
1289{
1290 PyErr_SetString(PyExc_TypeError, "set objects are unhashable");
1291 return -1;
1292}
1293
1294static int
1295set_nocmp(PyObject *self)
1296{
1297 PyErr_SetString(PyExc_TypeError, "cannot compare sets using cmp()");
1298 return -1;
1299}
1300
1301static long
1302frozenset_hash(PyObject *self)
1303{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001304 PyObject *key;
Raymond Hettingera690a992003-11-16 16:17:49 +00001305 PySetObject *so = (PySetObject *)self;
Raymond Hettingerdc5ae112003-12-13 14:46:46 +00001306 int pos = 0;
Raymond Hettingerc9786332004-06-10 22:41:48 +00001307 long hash = 1927868237L;
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001308
Raymond Hettingera690a992003-11-16 16:17:49 +00001309 if (so->hash != -1)
1310 return so->hash;
Raymond Hettingera690a992003-11-16 16:17:49 +00001311
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001312 hash *= set_len(self) + 1;
1313 while (set_next_internal(so, &pos, &key)) {
Raymond Hettingerc9786332004-06-10 22:41:48 +00001314 /* Work to increase the bit dispersion for closely spaced hash
1315 values. The is important because some use cases have many
1316 combinations of a small number of elements with nearby
1317 hashes so that many distinct combinations collapse to only
1318 a handful of distinct hash values. */
1319 long h = PyObject_Hash(key);
1320 hash ^= (h ^ (h << 16) ^ 89869747L) * 3644798167u;
Raymond Hettingera690a992003-11-16 16:17:49 +00001321 }
Raymond Hettingerc9786332004-06-10 22:41:48 +00001322 hash = hash * 69069L + 907133923L;
Raymond Hettinger27e403e2004-06-10 21:38:41 +00001323 if (hash == -1)
1324 hash = 590923713L;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001325 so->hash = hash;
Raymond Hettingera690a992003-11-16 16:17:49 +00001326 return hash;
1327}
1328
1329static PyObject *
1330set_richcompare(PySetObject *v, PyObject *w, int op)
1331{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001332 PyObject *r1, *r2;
1333
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001334 if(!PyAnySet_Check(w)) {
1335 if (op == Py_EQ)
1336 Py_RETURN_FALSE;
1337 if (op == Py_NE)
1338 Py_RETURN_TRUE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001339 PyErr_SetString(PyExc_TypeError, "can only compare to a set");
1340 return NULL;
1341 }
1342 switch (op) {
1343 case Py_EQ:
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001344 if (set_len((PyObject *)v) != set_len(w))
Raymond Hettingera690a992003-11-16 16:17:49 +00001345 Py_RETURN_FALSE;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001346 return set_issubset(v, w);
1347 case Py_NE:
1348 if (set_len((PyObject *)v) != set_len(w))
1349 Py_RETURN_TRUE;
1350 r1 = set_issubset(v, w);
1351 assert (r1 != NULL);
1352 r2 = PyBool_FromLong(PyObject_Not(r1));
1353 Py_DECREF(r1);
1354 return r2;
1355 case Py_LE:
1356 return set_issubset(v, w);
1357 case Py_GE:
1358 return set_issuperset(v, w);
1359 case Py_LT:
1360 if (set_len((PyObject *)v) >= set_len(w))
1361 Py_RETURN_FALSE;
1362 return set_issubset(v, w);
1363 case Py_GT:
1364 if (set_len((PyObject *)v) <= set_len(w))
1365 Py_RETURN_FALSE;
1366 return set_issuperset(v, w);
Raymond Hettingera690a992003-11-16 16:17:49 +00001367 }
1368 Py_INCREF(Py_NotImplemented);
1369 return Py_NotImplemented;
1370}
1371
1372static PyObject *
1373set_repr(PySetObject *so)
1374{
1375 PyObject *keys, *result, *listrepr;
1376
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001377 keys = PySequence_List((PyObject *)so);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001378 if (keys == NULL)
1379 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001380 listrepr = PyObject_Repr(keys);
1381 Py_DECREF(keys);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001382 if (listrepr == NULL)
1383 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001384
1385 result = PyString_FromFormat("%s(%s)", so->ob_type->tp_name,
1386 PyString_AS_STRING(listrepr));
1387 Py_DECREF(listrepr);
1388 return result;
1389}
1390
1391static int
1392set_tp_print(PySetObject *so, FILE *fp, int flags)
1393{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001394 PyObject *key;
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001395 int pos=0;
1396 char *emit = ""; /* No separator emitted on first pass */
1397 char *separator = ", ";
Raymond Hettingera690a992003-11-16 16:17:49 +00001398
Raymond Hettingera690a992003-11-16 16:17:49 +00001399 fprintf(fp, "%s([", so->ob_type->tp_name);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001400 while (set_next_internal(so, &pos, &key)) {
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001401 fputs(emit, fp);
1402 emit = separator;
Raymond Hettingerdc5ae112003-12-13 14:46:46 +00001403 if (PyObject_Print(key, fp, 0) != 0)
Raymond Hettingera690a992003-11-16 16:17:49 +00001404 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +00001405 }
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001406 fputs("])", fp);
Raymond Hettingera690a992003-11-16 16:17:49 +00001407 return 0;
1408}
1409
1410static PyObject *
1411set_clear(PySetObject *so)
1412{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001413 set_clear_internal(so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001414 so->hash = -1;
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001415 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001416}
1417
1418PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
1419
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001420static int
1421set_tp_clear(PySetObject *so)
1422{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001423 set_clear_internal(so);
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001424 so->hash = -1;
1425 return 0;
1426}
1427
Raymond Hettingera690a992003-11-16 16:17:49 +00001428static PyObject *
1429set_add(PySetObject *so, PyObject *item)
1430{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001431 if (set_add_internal(so, item) == -1)
Raymond Hettingera690a992003-11-16 16:17:49 +00001432 return NULL;
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001433 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001434}
1435
1436PyDoc_STRVAR(add_doc,
1437"Add an element to a set.\n\
1438\n\
1439This has no effect if the element is already present.");
1440
1441static PyObject *
1442set_remove(PySetObject *so, PyObject *item)
1443{
Raymond Hettinger0deab622003-12-13 18:53:18 +00001444 PyObject *tmp, *result;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001445 int rv;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001446
Raymond Hettinger0deab622003-12-13 18:53:18 +00001447 if (PyType_IsSubtype(item->ob_type, &PySet_Type)) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001448 tmp = make_new_set(&PyFrozenSet_Type, NULL);
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001449 if (tmp == NULL)
1450 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001451 set_swap_bodies((PySetObject *)item, (PySetObject *)tmp);
Raymond Hettinger0deab622003-12-13 18:53:18 +00001452 result = set_remove(so, tmp);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001453 set_swap_bodies((PySetObject *)item, (PySetObject *)tmp);
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001454 Py_DECREF(tmp);
Raymond Hettinger0deab622003-12-13 18:53:18 +00001455 return result;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001456 }
Raymond Hettinger0deab622003-12-13 18:53:18 +00001457
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001458 rv = set_discard_internal(so, item);
1459 if (rv == -1)
Raymond Hettinger0deab622003-12-13 18:53:18 +00001460 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001461 else if (rv == DISCARD_NOTFOUND) {
1462 PyErr_SetObject(PyExc_KeyError, item);
1463 return NULL;
1464 }
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001465 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001466}
1467
1468PyDoc_STRVAR(remove_doc,
1469"Remove an element from a set; it must be a member.\n\
1470\n\
1471If the element is not a member, raise a KeyError.");
1472
1473static PyObject *
1474set_discard(PySetObject *so, PyObject *item)
1475{
Raymond Hettinger0deab622003-12-13 18:53:18 +00001476 PyObject *tmp, *result;
1477
1478 if (PyType_IsSubtype(item->ob_type, &PySet_Type)) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001479 tmp = make_new_set(&PyFrozenSet_Type, NULL);
Raymond Hettinger0deab622003-12-13 18:53:18 +00001480 if (tmp == NULL)
1481 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001482 set_swap_bodies((PySetObject *)item, (PySetObject *)tmp);
Raymond Hettinger0deab622003-12-13 18:53:18 +00001483 result = set_discard(so, tmp);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001484 set_swap_bodies((PySetObject *)item, (PySetObject *)tmp);
Raymond Hettinger0deab622003-12-13 18:53:18 +00001485 Py_DECREF(tmp);
1486 return result;
1487 }
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001488
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001489 if (set_discard_internal(so, item) == -1)
1490 return NULL;
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001491 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001492}
1493
1494PyDoc_STRVAR(discard_doc,
1495"Remove an element from a set if it is a member.\n\
1496\n\
1497If the element is not a member, do nothing.");
1498
1499static PyObject *
1500set_pop(PySetObject *so)
1501{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001502 PyObject *key;
Raymond Hettingera690a992003-11-16 16:17:49 +00001503 int pos = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001504 int rv;
Raymond Hettingera690a992003-11-16 16:17:49 +00001505
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001506 if (!set_next_internal(so, &pos, &key)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001507 PyErr_SetString(PyExc_KeyError, "pop from an empty set");
1508 return NULL;
1509 }
1510 Py_INCREF(key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001511
1512 rv = set_discard_internal(so, key);
1513 if (rv == -1) {
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001514 Py_DECREF(key);
1515 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001516 } else if (rv == DISCARD_NOTFOUND) {
1517 Py_DECREF(key);
1518 PyErr_SetObject(PyExc_KeyError, key);
1519 return NULL;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001520 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001521 return key;
1522}
1523
1524PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.");
1525
1526static PyObject *
1527set_reduce(PySetObject *so)
1528{
Raymond Hettinger15056a52004-11-09 07:25:31 +00001529 PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001530
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001531 keys = PySequence_List((PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001532 if (keys == NULL)
1533 goto done;
1534 args = PyTuple_Pack(1, keys);
1535 if (args == NULL)
1536 goto done;
Raymond Hettinger15056a52004-11-09 07:25:31 +00001537 dict = PyObject_GetAttrString((PyObject *)so, "__dict__");
1538 if (dict == NULL) {
1539 PyErr_Clear();
1540 dict = Py_None;
1541 Py_INCREF(dict);
1542 }
1543 result = PyTuple_Pack(3, so->ob_type, args, dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001544done:
1545 Py_XDECREF(args);
1546 Py_XDECREF(keys);
Raymond Hettinger15056a52004-11-09 07:25:31 +00001547 Py_XDECREF(dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001548 return result;
1549}
1550
1551PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
1552
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001553static int
1554set_init(PySetObject *self, PyObject *args, PyObject *kwds)
1555{
1556 PyObject *iterable = NULL;
1557 PyObject *result;
1558
1559 if (!PyAnySet_Check(self))
1560 return -1;
1561 if (!PyArg_UnpackTuple(args, self->ob_type->tp_name, 0, 1, &iterable))
1562 return -1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001563 set_clear_internal(self);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001564 self->hash = -1;
1565 if (iterable == NULL)
1566 return 0;
Raymond Hettingera38123e2003-11-24 22:18:49 +00001567 result = set_update(self, iterable);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001568 if (result != NULL) {
1569 Py_DECREF(result);
1570 return 0;
1571 }
1572 return -1;
1573}
1574
Raymond Hettingera690a992003-11-16 16:17:49 +00001575static PySequenceMethods set_as_sequence = {
1576 (inquiry)set_len, /* sq_length */
1577 0, /* sq_concat */
1578 0, /* sq_repeat */
1579 0, /* sq_item */
1580 0, /* sq_slice */
1581 0, /* sq_ass_item */
1582 0, /* sq_ass_slice */
1583 (objobjproc)set_contains, /* sq_contains */
1584};
1585
1586/* set object ********************************************************/
1587
1588static PyMethodDef set_methods[] = {
1589 {"add", (PyCFunction)set_add, METH_O,
1590 add_doc},
1591 {"clear", (PyCFunction)set_clear, METH_NOARGS,
1592 clear_doc},
Raymond Hettinger0deab622003-12-13 18:53:18 +00001593 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001594 contains_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001595 {"copy", (PyCFunction)set_copy, METH_NOARGS,
1596 copy_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001597 {"discard", (PyCFunction)set_discard, METH_O,
1598 discard_doc},
1599 {"difference", (PyCFunction)set_difference, METH_O,
1600 difference_doc},
1601 {"difference_update", (PyCFunction)set_difference_update, METH_O,
1602 difference_update_doc},
1603 {"intersection",(PyCFunction)set_intersection, METH_O,
1604 intersection_doc},
1605 {"intersection_update",(PyCFunction)set_intersection_update, METH_O,
1606 intersection_update_doc},
1607 {"issubset", (PyCFunction)set_issubset, METH_O,
1608 issubset_doc},
1609 {"issuperset", (PyCFunction)set_issuperset, METH_O,
1610 issuperset_doc},
1611 {"pop", (PyCFunction)set_pop, METH_NOARGS,
1612 pop_doc},
1613 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
1614 reduce_doc},
1615 {"remove", (PyCFunction)set_remove, METH_O,
1616 remove_doc},
1617 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
1618 symmetric_difference_doc},
1619 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
1620 symmetric_difference_update_doc},
1621 {"union", (PyCFunction)set_union, METH_O,
1622 union_doc},
Raymond Hettingera38123e2003-11-24 22:18:49 +00001623 {"update", (PyCFunction)set_update, METH_O,
1624 update_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001625 {NULL, NULL} /* sentinel */
1626};
1627
1628static PyNumberMethods set_as_number = {
1629 0, /*nb_add*/
1630 (binaryfunc)set_sub, /*nb_subtract*/
1631 0, /*nb_multiply*/
1632 0, /*nb_divide*/
1633 0, /*nb_remainder*/
1634 0, /*nb_divmod*/
1635 0, /*nb_power*/
1636 0, /*nb_negative*/
1637 0, /*nb_positive*/
1638 0, /*nb_absolute*/
1639 0, /*nb_nonzero*/
1640 0, /*nb_invert*/
1641 0, /*nb_lshift*/
1642 0, /*nb_rshift*/
1643 (binaryfunc)set_and, /*nb_and*/
1644 (binaryfunc)set_xor, /*nb_xor*/
1645 (binaryfunc)set_or, /*nb_or*/
1646 0, /*nb_coerce*/
1647 0, /*nb_int*/
1648 0, /*nb_long*/
1649 0, /*nb_float*/
1650 0, /*nb_oct*/
1651 0, /*nb_hex*/
1652 0, /*nb_inplace_add*/
1653 (binaryfunc)set_isub, /*nb_inplace_subtract*/
1654 0, /*nb_inplace_multiply*/
1655 0, /*nb_inplace_divide*/
1656 0, /*nb_inplace_remainder*/
1657 0, /*nb_inplace_power*/
1658 0, /*nb_inplace_lshift*/
1659 0, /*nb_inplace_rshift*/
1660 (binaryfunc)set_iand, /*nb_inplace_and*/
1661 (binaryfunc)set_ixor, /*nb_inplace_xor*/
1662 (binaryfunc)set_ior, /*nb_inplace_or*/
1663};
1664
1665PyDoc_STRVAR(set_doc,
1666"set(iterable) --> set object\n\
1667\n\
1668Build an unordered collection.");
1669
1670PyTypeObject PySet_Type = {
1671 PyObject_HEAD_INIT(&PyType_Type)
1672 0, /* ob_size */
1673 "set", /* tp_name */
1674 sizeof(PySetObject), /* tp_basicsize */
1675 0, /* tp_itemsize */
1676 /* methods */
1677 (destructor)set_dealloc, /* tp_dealloc */
1678 (printfunc)set_tp_print, /* tp_print */
1679 0, /* tp_getattr */
1680 0, /* tp_setattr */
1681 (cmpfunc)set_nocmp, /* tp_compare */
1682 (reprfunc)set_repr, /* tp_repr */
1683 &set_as_number, /* tp_as_number */
1684 &set_as_sequence, /* tp_as_sequence */
1685 0, /* tp_as_mapping */
1686 set_nohash, /* tp_hash */
1687 0, /* tp_call */
1688 0, /* tp_str */
1689 PyObject_GenericGetAttr, /* tp_getattro */
1690 0, /* tp_setattro */
1691 0, /* tp_as_buffer */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001692 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES |
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001693 Py_TPFLAGS_BASETYPE, /* tp_flags */
Raymond Hettingera690a992003-11-16 16:17:49 +00001694 set_doc, /* tp_doc */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001695 (traverseproc)set_traverse, /* tp_traverse */
1696 (inquiry)set_tp_clear, /* tp_clear */
Raymond Hettingera690a992003-11-16 16:17:49 +00001697 (richcmpfunc)set_richcompare, /* tp_richcompare */
Raymond Hettinger691d8052004-05-30 07:26:47 +00001698 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001699 (getiterfunc)set_iter, /* tp_iter */
Raymond Hettingera690a992003-11-16 16:17:49 +00001700 0, /* tp_iternext */
1701 set_methods, /* tp_methods */
1702 0, /* tp_members */
1703 0, /* tp_getset */
1704 0, /* tp_base */
1705 0, /* tp_dict */
1706 0, /* tp_descr_get */
1707 0, /* tp_descr_set */
1708 0, /* tp_dictoffset */
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001709 (initproc)set_init, /* tp_init */
Raymond Hettingera690a992003-11-16 16:17:49 +00001710 PyType_GenericAlloc, /* tp_alloc */
1711 set_new, /* tp_new */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001712 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00001713};
1714
1715/* frozenset object ********************************************************/
1716
1717
1718static PyMethodDef frozenset_methods[] = {
Raymond Hettinger0deab622003-12-13 18:53:18 +00001719 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001720 contains_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001721 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
Raymond Hettingera690a992003-11-16 16:17:49 +00001722 copy_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001723 {"difference", (PyCFunction)set_difference, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00001724 difference_doc},
1725 {"intersection",(PyCFunction)set_intersection, METH_O,
1726 intersection_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001727 {"issubset", (PyCFunction)set_issubset, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00001728 issubset_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001729 {"issuperset", (PyCFunction)set_issuperset, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00001730 issuperset_doc},
1731 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
1732 reduce_doc},
1733 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
1734 symmetric_difference_doc},
1735 {"union", (PyCFunction)set_union, METH_O,
1736 union_doc},
1737 {NULL, NULL} /* sentinel */
1738};
1739
1740static PyNumberMethods frozenset_as_number = {
1741 0, /*nb_add*/
1742 (binaryfunc)set_sub, /*nb_subtract*/
1743 0, /*nb_multiply*/
1744 0, /*nb_divide*/
1745 0, /*nb_remainder*/
1746 0, /*nb_divmod*/
1747 0, /*nb_power*/
1748 0, /*nb_negative*/
1749 0, /*nb_positive*/
1750 0, /*nb_absolute*/
1751 0, /*nb_nonzero*/
1752 0, /*nb_invert*/
1753 0, /*nb_lshift*/
1754 0, /*nb_rshift*/
1755 (binaryfunc)set_and, /*nb_and*/
1756 (binaryfunc)set_xor, /*nb_xor*/
1757 (binaryfunc)set_or, /*nb_or*/
1758};
1759
1760PyDoc_STRVAR(frozenset_doc,
1761"frozenset(iterable) --> frozenset object\n\
1762\n\
1763Build an immutable unordered collection.");
1764
1765PyTypeObject PyFrozenSet_Type = {
1766 PyObject_HEAD_INIT(&PyType_Type)
1767 0, /* ob_size */
1768 "frozenset", /* tp_name */
1769 sizeof(PySetObject), /* tp_basicsize */
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001770 0, /* tp_itemsize */
1771 /* methods */
Raymond Hettingera690a992003-11-16 16:17:49 +00001772 (destructor)set_dealloc, /* tp_dealloc */
1773 (printfunc)set_tp_print, /* tp_print */
1774 0, /* tp_getattr */
1775 0, /* tp_setattr */
1776 (cmpfunc)set_nocmp, /* tp_compare */
1777 (reprfunc)set_repr, /* tp_repr */
1778 &frozenset_as_number, /* tp_as_number */
1779 &set_as_sequence, /* tp_as_sequence */
1780 0, /* tp_as_mapping */
1781 frozenset_hash, /* tp_hash */
1782 0, /* tp_call */
1783 0, /* tp_str */
1784 PyObject_GenericGetAttr, /* tp_getattro */
1785 0, /* tp_setattro */
1786 0, /* tp_as_buffer */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001787 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES |
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001788 Py_TPFLAGS_BASETYPE, /* tp_flags */
Raymond Hettingera690a992003-11-16 16:17:49 +00001789 frozenset_doc, /* tp_doc */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001790 (traverseproc)set_traverse, /* tp_traverse */
Raymond Hettingera690a992003-11-16 16:17:49 +00001791 0, /* tp_clear */
1792 (richcmpfunc)set_richcompare, /* tp_richcompare */
Raymond Hettinger691d8052004-05-30 07:26:47 +00001793 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
Raymond Hettingera690a992003-11-16 16:17:49 +00001794 (getiterfunc)set_iter, /* tp_iter */
1795 0, /* tp_iternext */
1796 frozenset_methods, /* tp_methods */
1797 0, /* tp_members */
1798 0, /* tp_getset */
1799 0, /* tp_base */
1800 0, /* tp_dict */
1801 0, /* tp_descr_get */
1802 0, /* tp_descr_set */
1803 0, /* tp_dictoffset */
1804 0, /* tp_init */
1805 PyType_GenericAlloc, /* tp_alloc */
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001806 frozenset_new, /* tp_new */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001807 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00001808};