blob: 61ba8539b664f08090a9e97b5fd360c8a9820175 [file] [log] [blame]
Raymond Hettingera690a992003-11-16 16:17:49 +00001#include "Python.h"
2
3/* set object implementation
4 written and maintained by Raymond D. Hettinger <python@rcn.com>
5 derived from sets.py written by Greg V. Wilson, Alex Martelli,
6 Guido van Rossum, Raymond Hettinger, and Tim Peters.
7
8 Copyright (c) 2003 Python Software Foundation.
9 All rights reserved.
10*/
11
12/* Fast access macros */
13
14#define DICT_CONTAINS(d, k) (d->ob_type->tp_as_sequence->sq_contains(d, k))
15#define IS_SET(so) (so->ob_type == &PySet_Type || so->ob_type == &PyFrozenSet_Type)
16
17/* set object **********************************************************/
18
19static PyObject *
20make_new_set(PyTypeObject *type, PyObject *iterable)
21{
22 PyObject *data;
23 PyObject *it = NULL;
24 PyObject *item;
25 PySetObject *so;
26
27 /* Get iterator. */
28 if (iterable != NULL) {
29 it = PyObject_GetIter(iterable);
30 if (it == NULL)
31 return NULL;
32 }
33
34 data = PyDict_New();
35 if (data == NULL) {
36 Py_DECREF(it);
37 return NULL;
38 }
39
40 while (it != NULL && (item = PyIter_Next(it)) != NULL) {
41 if (PyDict_SetItem(data, item, Py_True) == -1) {
42 Py_DECREF(it);
43 Py_DECREF(data);
44 Py_DECREF(item);
45 PyErr_SetString(PyExc_TypeError,
46 "all set entries must be immutable");
47 return NULL;
48 }
49 Py_DECREF(item);
50 }
51 Py_XDECREF(it);
52 if (PyErr_Occurred()) {
53 Py_DECREF(data);
54 return NULL;
55 }
56
57 /* create PySetObject structure */
58 so = (PySetObject *)type->tp_alloc(type, 0);
59 if (so == NULL) {
60 Py_DECREF(data);
61 return NULL;
62 }
63 so->data = data;
64 so->hash = -1;
65
66 return (PyObject *)so;
67}
68
69static PyObject *
70set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
71{
72 PyObject *iterable = NULL;
73
74 if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
75 return NULL;
76 return make_new_set(type, iterable);
77}
78
79static void
80set_dealloc(PySetObject *so)
81{
82 PyObject_GC_UnTrack(so);
83 Py_XDECREF(so->data);
84 so->ob_type->tp_free(so);
85}
86
87static int
88set_traverse(PySetObject *so, visitproc visit, void *arg)
89{
90 if (so->data)
91 return visit(so->data, arg);
92 return 0;
93}
94
95static PyObject *
96set_iter(PySetObject *so)
97{
98 return PyObject_GetIter(so->data);
99}
100
101static int
102set_len(PySetObject *so)
103{
104 return PyDict_Size(so->data);
105}
106
107static int
108set_contains(PySetObject *so, PyObject *key)
109{
110 return DICT_CONTAINS(so->data, key);
111}
112
113static PyObject *
114set_copy(PySetObject *so)
115{
116 PyObject *data;
117 PySetObject *newso;
118
119 data = PyDict_Copy(so->data);
120 if (data == NULL)
121 return NULL;
122
123 newso = (PySetObject *)(so->ob_type->tp_alloc(so->ob_type, 0));
124 if (newso == NULL) {
125 Py_DECREF(data);
126 return NULL;
127 }
128 newso->data = data;
129 newso->hash = so->hash;
130 return (PyObject *)newso;
131}
132
133PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
134
135static PyObject *
136set_union(PySetObject *so, PyObject *other)
137{
138 PySetObject *result;
139 PyObject *item, *data, *it;
140
141 result = (PySetObject *)set_copy(so);
142 it = PyObject_GetIter(other);
143 if (it == NULL) {
144 Py_DECREF(result);
145 return NULL;
146 }
147 data = result->data;
148 while ((item = PyIter_Next(it)) != NULL) {
149 if (PyDict_SetItem(data, item, Py_True) == -1) {
150 Py_DECREF(it);
151 Py_DECREF(result);
152 Py_DECREF(item);
153 PyErr_SetString(PyExc_TypeError,
154 "all set entries must be immutable");
155 return NULL;
156 }
157 Py_DECREF(item);
158 }
159 Py_DECREF(it);
160 if (PyErr_Occurred()) {
161 Py_DECREF(result);
162 return NULL;
163 }
164 return (PyObject *)result;
165}
166
167PyDoc_STRVAR(union_doc,
168 "Return the union of two sets as a new set.\n\
169\n\
170(i.e. all elements that are in either set.)");
171
172static PyObject *
173set_union_update(PySetObject *so, PyObject *other)
174{
175 PyObject *item, *data, *it;
176
177 it = PyObject_GetIter(other);
178 if (it == NULL)
179 return NULL;
180 data = so->data;
181
182 while ((item = PyIter_Next(it)) != NULL) {
183 if (PyDict_SetItem(data, item, Py_True) == -1) {
184 Py_DECREF(it);
185 Py_DECREF(item);
186 PyErr_SetString(PyExc_TypeError,
187 "all set entries must be immutable");
188 return NULL;
189 }
190 Py_DECREF(item);
191 }
192 Py_DECREF(it);
193 if (PyErr_Occurred())
194 return NULL;
195 Py_RETURN_NONE;
196}
197
198PyDoc_STRVAR(union_update_doc,
199"Update a set with the union of itself and another.");
200
201static PyObject *
202set_or(PySetObject *so, PyObject *other)
203{
204 if (!IS_SET(so) || !IS_SET(other)) {
205 Py_INCREF(Py_NotImplemented);
206 return Py_NotImplemented;
207 }
208 return set_union(so, other);
209}
210
211static PyObject *
212set_ior(PySetObject *so, PyObject *other)
213{
214 PyObject *result;
215
216 if (!IS_SET(other)) {
217 Py_INCREF(Py_NotImplemented);
218 return Py_NotImplemented;
219 }
220 result = set_union_update(so, other);
221 if (result == NULL)
222 return NULL;
223 Py_DECREF(result);
224 Py_INCREF(so);
225 return (PyObject *)so;
226}
227
228static PyObject *
229set_intersection(PySetObject *so, PyObject *other)
230{
231 PySetObject *result;
232 PyObject *item, *selfdata, *tgtdata, *it;
233
234 result = (PySetObject *)make_new_set(so->ob_type, NULL);
235 if (result == NULL)
236 return NULL;
237
238 it = PyObject_GetIter(other);
239 if (it == NULL) {
240 Py_DECREF(result);
241 return NULL;
242 }
243
244 selfdata = so->data;
245 tgtdata = result->data;
246 while ((item = PyIter_Next(it)) != NULL) {
247 if (DICT_CONTAINS(selfdata, item)) {
248 if (PyDict_SetItem(tgtdata, item, Py_True) == -1) {
249 Py_DECREF(it);
250 Py_DECREF(result);
251 Py_DECREF(item);
252 PyErr_SetString(PyExc_TypeError,
253 "all set entries must be immutable");
254 return NULL;
255 }
256 }
257 Py_DECREF(item);
258 }
259 Py_DECREF(it);
260 if (PyErr_Occurred()) {
261 Py_DECREF(result);
262 return NULL;
263 }
264 return (PyObject *)result;
265}
266
267PyDoc_STRVAR(intersection_doc,
268"Return the intersection of two sets as a new set.\n\
269\n\
270(i.e. all elements that are in both sets.)");
271
272static PyObject *
273set_intersection_update(PySetObject *so, PyObject *other)
274{
275 PyObject *item, *selfdata, *it, *newdict, *tmp;
276
277 newdict = PyDict_New();
278 if (newdict == NULL)
279 return newdict;
280
281 it = PyObject_GetIter(other);
282 if (it == NULL) {
283 Py_DECREF(newdict);
284 return NULL;
285 }
286
287 selfdata = so->data;
288 while ((item = PyIter_Next(it)) != NULL) {
289 if (DICT_CONTAINS(selfdata, item)) {
290 if (PyDict_SetItem(newdict, item, Py_True) == -1) {
291 Py_DECREF(newdict);
292 Py_DECREF(it);
293 Py_DECREF(item);
294 PyErr_SetString(PyExc_TypeError,
295 "all set entries must be immutable");
296 return NULL;
297 }
298 }
299 Py_DECREF(item);
300 }
301 Py_DECREF(it);
302 if (PyErr_Occurred()) {
303 Py_DECREF(newdict);
304 return NULL;
305 }
306 tmp = so->data;
307 so->data = newdict;
308 Py_DECREF(tmp);
309 Py_RETURN_NONE;
310}
311
312PyDoc_STRVAR(intersection_update_doc,
313"Update a set with the intersection of itself and another.");
314
315static PyObject *
316set_and(PySetObject *so, PyObject *other)
317{
318 if (!IS_SET(so) || !IS_SET(other)) {
319 Py_INCREF(Py_NotImplemented);
320 return Py_NotImplemented;
321 }
322 return set_intersection(so, other);
323}
324
325static PyObject *
326set_iand(PySetObject *so, PyObject *other)
327{
328 PyObject *result;
329
330 if (!IS_SET(other)) {
331 Py_INCREF(Py_NotImplemented);
332 return Py_NotImplemented;
333 }
334 result = set_intersection_update(so, other);
335 if (result == NULL)
336 return NULL;
337 Py_DECREF(result);
338 Py_INCREF(so);
339 return (PyObject *)so;
340}
341
342static PyObject *
343set_difference(PySetObject *so, PyObject *other)
344{
345 PySetObject *result;
346 PyObject *item, *tgtdata, *it;
347
348 result = (PySetObject *)set_copy(so);
349 if (result == NULL)
350 return NULL;
351
352 it = PyObject_GetIter(other);
353 if (it == NULL) {
354 Py_DECREF(result);
355 return NULL;
356 }
357
358 tgtdata = result->data;
359 while ((item = PyIter_Next(it)) != NULL) {
360 if (PyDict_DelItem(tgtdata, item) == -1) {
361 if (PyErr_ExceptionMatches(PyExc_KeyError))
362 PyErr_Clear();
363 else {
364 Py_DECREF(it);
365 Py_DECREF(result);
366 Py_DECREF(item);
367 return NULL;
368 }
369 }
370 Py_DECREF(item);
371 }
372 Py_DECREF(it);
373 if (PyErr_Occurred()) {
374 Py_DECREF(result);
375 return NULL;
376 }
377 return (PyObject *)result;
378}
379
380PyDoc_STRVAR(difference_doc,
381"Return the difference of two sets as a new set.\n\
382\n\
383(i.e. all elements that are in this set but not the other.)");
384
385static PyObject *
386set_difference_update(PySetObject *so, PyObject *other)
387{
388 PyObject *item, *tgtdata, *it;
389
390 it = PyObject_GetIter(other);
391 if (it == NULL)
392 return NULL;
393
394 tgtdata = so->data;
395 while ((item = PyIter_Next(it)) != NULL) {
396 if (PyDict_DelItem(tgtdata, item) == -1) {
397 if (PyErr_ExceptionMatches(PyExc_KeyError))
398 PyErr_Clear();
399 else {
400 Py_DECREF(it);
401 Py_DECREF(item);
402 return NULL;
403 }
404 }
405 Py_DECREF(item);
406 }
407 Py_DECREF(it);
408 if (PyErr_Occurred())
409 return NULL;
410 Py_RETURN_NONE;
411}
412
413PyDoc_STRVAR(difference_update_doc,
414"Remove all elements of another set from this set.");
415
416static PyObject *
417set_sub(PySetObject *so, PyObject *other)
418{
419 if (!IS_SET(so) || !IS_SET(other)) {
420 Py_INCREF(Py_NotImplemented);
421 return Py_NotImplemented;
422 }
423 return set_difference(so, other);
424}
425
426static PyObject *
427set_isub(PySetObject *so, PyObject *other)
428{
429 PyObject *result;
430
431 if (!IS_SET(other)) {
432 Py_INCREF(Py_NotImplemented);
433 return Py_NotImplemented;
434 }
435 result = set_difference_update(so, other);
436 if (result == NULL)
437 return NULL;
438 Py_DECREF(result);
439 Py_INCREF(so);
440 return (PyObject *)so;
441}
442
443static PyObject *
444set_symmetric_difference(PySetObject *so, PyObject *other)
445{
446 PySetObject *result, *otherset;
447 PyObject *item, *selfdata, *otherdata, *tgtdata, *it;
448
449 selfdata = so->data;
450
451 result = (PySetObject *)set_copy(so);
452 if (result == NULL)
453 return NULL;
454 tgtdata = result->data;
455
456 otherset = (PySetObject *)make_new_set(so->ob_type, other);
457 if (otherset == NULL) {
458 Py_DECREF(result);
459 return NULL;
460 }
461 otherdata = otherset->data;
462
463 it = PyObject_GetIter(otherdata);
464 if (it == NULL) {
465 Py_DECREF(otherset);
466 Py_DECREF(result);
467 return NULL;
468 }
469
470 while ((item = PyIter_Next(it)) != NULL) {
471 if (PyDict_DelItem(tgtdata, item) == -1) {
472 PyErr_Clear();
473 if (PyDict_SetItem(tgtdata, item, Py_True) == -1) {
474 Py_DECREF(it);
475 Py_DECREF(otherset);
476 Py_DECREF(result);
477 Py_DECREF(item);
478 PyErr_SetString(PyExc_TypeError,
479 "all set entries must be immutable");
480 return NULL;
481 }
482 }
483 Py_DECREF(item);
484 }
485 Py_DECREF(it);
486 Py_DECREF(otherset);
487 if (PyErr_Occurred()) {
488 Py_DECREF(result);
489 return NULL;
490 }
491 return (PyObject *)result;
492}
493
494PyDoc_STRVAR(symmetric_difference_doc,
495"Return the symmetric difference of two sets as a new set.\n\
496\n\
497(i.e. all elements that are in exactly one of the sets.)");
498
499static PyObject *
500set_symmetric_difference_update(PySetObject *so, PyObject *other)
501{
502 PyObject *item, *selfdata, *it, *otherdata;
503 PySetObject *otherset = NULL;
504
505 selfdata = so->data;
506
507 if (PyDict_Check(other))
508 otherdata = other;
509 else if (IS_SET(other))
510 otherdata = ((PySetObject *)other)->data;
511 else {
512 otherset = (PySetObject *)make_new_set(so->ob_type, other);
513 if (otherset == NULL)
514 return NULL;
515 otherdata = otherset->data;
516 }
517
518 it = PyObject_GetIter(otherdata);
519 if (it == NULL)
520 return NULL;
521
522 while ((item = PyIter_Next(it)) != NULL) {
523 if (DICT_CONTAINS(selfdata, item)) {
524 if (PyDict_DelItem(selfdata, item) == -1) {
525 Py_XDECREF(otherset);
526 Py_DECREF(it);
527 Py_DECREF(item);
528 PyErr_SetString(PyExc_TypeError,
529 "all set entries must be immutable");
530 return NULL;
531 }
532 } else {
533 if (PyDict_SetItem(selfdata, item, Py_True) == -1) {
534 Py_XDECREF(otherset);
535 Py_DECREF(it);
536 Py_DECREF(item);
537 PyErr_SetString(PyExc_TypeError,
538 "all set entries must be immutable");
539 return NULL;
540 }
541 }
542 Py_DECREF(item);
543 }
544 Py_XDECREF(otherset);
545 Py_DECREF(it);
546 if (PyErr_Occurred())
547 return NULL;
548 Py_RETURN_NONE;
549}
550
551PyDoc_STRVAR(symmetric_difference_update_doc,
552"Update a set with the symmetric difference of itself and another.");
553
554static PyObject *
555set_xor(PySetObject *so, PyObject *other)
556{
557 if (!IS_SET(so) || !IS_SET(other)) {
558 Py_INCREF(Py_NotImplemented);
559 return Py_NotImplemented;
560 }
561 return set_symmetric_difference(so, other);
562}
563
564static PyObject *
565set_ixor(PySetObject *so, PyObject *other)
566{
567 PyObject *result;
568
569 if (!IS_SET(other)) {
570 Py_INCREF(Py_NotImplemented);
571 return Py_NotImplemented;
572 }
573 result = set_symmetric_difference_update(so, other);
574 if (result == NULL)
575 return NULL;
576 Py_DECREF(result);
577 Py_INCREF(so);
578 return (PyObject *)so;
579}
580
581static PyObject *
582set_issubset(PySetObject *so, PyObject *other)
583{
584 PyObject *otherdata, *it, *item;
585
586 if (!IS_SET(other)) {
587 PyErr_SetString(PyExc_TypeError, "can only compare to a set");
588 return NULL;
589 }
590 if (set_len(so) > set_len((PySetObject *)other))
591 Py_RETURN_FALSE;
592
593 it = PyObject_GetIter(so->data);
594 if (it == NULL)
595 return NULL;
596
597 otherdata = ((PySetObject *)other)->data;
598 while ((item = PyIter_Next(it)) != NULL) {
599 if (!DICT_CONTAINS(otherdata, item)) {
600 Py_DECREF(it);
601 Py_DECREF(item);
602 Py_RETURN_FALSE;
603 }
604 Py_DECREF(item);
605 }
606 Py_DECREF(it);
607 Py_RETURN_TRUE;
608}
609
610PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
611
612static PyObject *
613set_issuperset(PySetObject *so, PyObject *other)
614{
615 if (!IS_SET(other)) {
616 PyErr_SetString(PyExc_TypeError, "can only compare to a set");
617 return NULL;
618 }
619 return set_issubset((PySetObject *)other, (PyObject *)so);
620}
621
622PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
623
624static long
625set_nohash(PyObject *self)
626{
627 PyErr_SetString(PyExc_TypeError, "set objects are unhashable");
628 return -1;
629}
630
631static int
632set_nocmp(PyObject *self)
633{
634 PyErr_SetString(PyExc_TypeError, "cannot compare sets using cmp()");
635 return -1;
636}
637
638static long
639frozenset_hash(PyObject *self)
640{
641 PyObject *it, *item;
642 PySetObject *so = (PySetObject *)self;
643 long hash = 0;
644
645 if (so->hash != -1)
646 return so->hash;
647
648 it = PyObject_GetIter(((PySetObject *)so)->data);
649 if (it == NULL)
650 return -1;
651
652 while ((item = PyIter_Next(it)) != NULL) {
653 hash ^= PyObject_Hash(item);
654 Py_DECREF(item);
655 }
656 so->hash = hash;
657 Py_DECREF(it);
658 return hash;
659}
660
661static PyObject *
662set_richcompare(PySetObject *v, PyObject *w, int op)
663{
664 /* XXX factor out is_set test */
665 if (op == Py_EQ && !IS_SET(w))
666 Py_RETURN_FALSE;
667 else if (op == Py_NE && !IS_SET(w))
668 Py_RETURN_TRUE;
669 if (!IS_SET(w)) {
670 PyErr_SetString(PyExc_TypeError, "can only compare to a set");
671 return NULL;
672 }
673 switch (op) {
674 case Py_EQ:
675 case Py_NE:
676 return PyObject_RichCompare(((PySetObject *)v)->data,
677 ((PySetObject *)w)->data, op);
678 case Py_LE:
679 return set_issubset((PySetObject *)v, w);
680 case Py_GE:
681 return set_issuperset((PySetObject *)v, w);
682 case Py_LT:
683 if (set_len(v) >= set_len((PySetObject *)w))
684 Py_RETURN_FALSE;
685 return set_issubset((PySetObject *)v, w);
686 case Py_GT:
687 if (set_len(v) <= set_len((PySetObject *)w))
688 Py_RETURN_FALSE;
689 return set_issuperset((PySetObject *)v, w);
690 }
691 Py_INCREF(Py_NotImplemented);
692 return Py_NotImplemented;
693}
694
695static PyObject *
696set_repr(PySetObject *so)
697{
698 PyObject *keys, *result, *listrepr;
699
700 keys = PyDict_Keys(so->data);
701 listrepr = PyObject_Repr(keys);
702 Py_DECREF(keys);
703
704 result = PyString_FromFormat("%s(%s)", so->ob_type->tp_name,
705 PyString_AS_STRING(listrepr));
706 Py_DECREF(listrepr);
707 return result;
708}
709
710static int
711set_tp_print(PySetObject *so, FILE *fp, int flags)
712{
713 PyObject *it, *item;
714 int firstpass=1;
715
716 it = PyObject_GetIter(so->data);
717 if (it == NULL)
718 return -1;
719 fprintf(fp, "%s([", so->ob_type->tp_name);
720
721 while ((item = PyIter_Next(it)) != NULL) {
722 if (firstpass == 1)
723 firstpass = 0;
724 else
Raymond Hettingere2c277a2003-11-16 16:36:58 +0000725 fprintf(fp, ", ");
Raymond Hettingera690a992003-11-16 16:17:49 +0000726 if (PyObject_Print(item, fp, 0) != 0) {
727 Py_DECREF(it);
728 Py_DECREF(item);
729 return -1;
730 }
731 Py_DECREF(item);
732 }
733 Py_DECREF(it);
734 fprintf(fp, "])");
735 return 0;
736}
737
738static PyObject *
739set_clear(PySetObject *so)
740{
741 PyDict_Clear(so->data);
742 so->hash = -1;
743 Py_INCREF(Py_None);
744 return Py_None;
745}
746
747PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
748
749static int
750set_tp_clear(PySetObject *so)
751{
752 PyDict_Clear(so->data);
753 so->hash = -1;
754 return 0;
755}
756
757static PyObject *
758set_add(PySetObject *so, PyObject *item)
759{
760 if (PyDict_SetItem(so->data, item, Py_True) == -1)
761 return NULL;
762 Py_INCREF(Py_None);
763 return Py_None;
764}
765
766PyDoc_STRVAR(add_doc,
767"Add an element to a set.\n\
768\n\
769This has no effect if the element is already present.");
770
771static PyObject *
772set_remove(PySetObject *so, PyObject *item)
773{
774 if (PyDict_DelItem(so->data, item) == -1)
775 return NULL;
776 Py_INCREF(Py_None);
777 return Py_None;
778}
779
780PyDoc_STRVAR(remove_doc,
781"Remove an element from a set; it must be a member.\n\
782\n\
783If the element is not a member, raise a KeyError.");
784
785static PyObject *
786set_discard(PySetObject *so, PyObject *item)
787{
788 if (PyDict_DelItem(so->data, item) == -1)
789 if (PyErr_ExceptionMatches(PyExc_KeyError))
790 PyErr_Clear();
791 else
792 return NULL;
793 Py_INCREF(Py_None);
794 return Py_None;
795}
796
797PyDoc_STRVAR(discard_doc,
798"Remove an element from a set if it is a member.\n\
799\n\
800If the element is not a member, do nothing.");
801
802static PyObject *
803set_pop(PySetObject *so)
804{
805 PyObject *key, *value;
806 int pos = 0;
807
808 if (!PyDict_Next(so->data, &pos, &key, &value)) {
809 PyErr_SetString(PyExc_KeyError, "pop from an empty set");
810 return NULL;
811 }
812 Py_INCREF(key);
813 if (PyDict_DelItem(so->data, key) == -1)
814 PyErr_Clear();
815 return key;
816}
817
818PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.");
819
820static PyObject *
821set_reduce(PySetObject *so)
822{
823 PyObject *keys=NULL, *args=NULL, *result=NULL;
824
825 keys = PyDict_Keys(so->data);
826 if (keys == NULL)
827 goto done;
828 args = PyTuple_Pack(1, keys);
829 if (args == NULL)
830 goto done;
831 result = PyTuple_Pack(2, so->ob_type, args);
832done:
833 Py_XDECREF(args);
834 Py_XDECREF(keys);
835 return result;
836}
837
838PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
839
840static PySequenceMethods set_as_sequence = {
841 (inquiry)set_len, /* sq_length */
842 0, /* sq_concat */
843 0, /* sq_repeat */
844 0, /* sq_item */
845 0, /* sq_slice */
846 0, /* sq_ass_item */
847 0, /* sq_ass_slice */
848 (objobjproc)set_contains, /* sq_contains */
849};
850
851/* set object ********************************************************/
852
853static PyMethodDef set_methods[] = {
854 {"add", (PyCFunction)set_add, METH_O,
855 add_doc},
856 {"clear", (PyCFunction)set_clear, METH_NOARGS,
857 clear_doc},
858 {"copy", (PyCFunction)set_copy, METH_NOARGS,
859 copy_doc},
860 {"__copy__", (PyCFunction)set_copy, METH_NOARGS,
861 copy_doc},
862 {"discard", (PyCFunction)set_discard, METH_O,
863 discard_doc},
864 {"difference", (PyCFunction)set_difference, METH_O,
865 difference_doc},
866 {"difference_update", (PyCFunction)set_difference_update, METH_O,
867 difference_update_doc},
868 {"intersection",(PyCFunction)set_intersection, METH_O,
869 intersection_doc},
870 {"intersection_update",(PyCFunction)set_intersection_update, METH_O,
871 intersection_update_doc},
872 {"issubset", (PyCFunction)set_issubset, METH_O,
873 issubset_doc},
874 {"issuperset", (PyCFunction)set_issuperset, METH_O,
875 issuperset_doc},
876 {"pop", (PyCFunction)set_pop, METH_NOARGS,
877 pop_doc},
878 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
879 reduce_doc},
880 {"remove", (PyCFunction)set_remove, METH_O,
881 remove_doc},
882 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
883 symmetric_difference_doc},
884 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
885 symmetric_difference_update_doc},
886 {"union", (PyCFunction)set_union, METH_O,
887 union_doc},
888 {"union_update",(PyCFunction)set_union_update, METH_O,
889 union_update_doc},
890 {NULL, NULL} /* sentinel */
891};
892
893static PyNumberMethods set_as_number = {
894 0, /*nb_add*/
895 (binaryfunc)set_sub, /*nb_subtract*/
896 0, /*nb_multiply*/
897 0, /*nb_divide*/
898 0, /*nb_remainder*/
899 0, /*nb_divmod*/
900 0, /*nb_power*/
901 0, /*nb_negative*/
902 0, /*nb_positive*/
903 0, /*nb_absolute*/
904 0, /*nb_nonzero*/
905 0, /*nb_invert*/
906 0, /*nb_lshift*/
907 0, /*nb_rshift*/
908 (binaryfunc)set_and, /*nb_and*/
909 (binaryfunc)set_xor, /*nb_xor*/
910 (binaryfunc)set_or, /*nb_or*/
911 0, /*nb_coerce*/
912 0, /*nb_int*/
913 0, /*nb_long*/
914 0, /*nb_float*/
915 0, /*nb_oct*/
916 0, /*nb_hex*/
917 0, /*nb_inplace_add*/
918 (binaryfunc)set_isub, /*nb_inplace_subtract*/
919 0, /*nb_inplace_multiply*/
920 0, /*nb_inplace_divide*/
921 0, /*nb_inplace_remainder*/
922 0, /*nb_inplace_power*/
923 0, /*nb_inplace_lshift*/
924 0, /*nb_inplace_rshift*/
925 (binaryfunc)set_iand, /*nb_inplace_and*/
926 (binaryfunc)set_ixor, /*nb_inplace_xor*/
927 (binaryfunc)set_ior, /*nb_inplace_or*/
928};
929
930PyDoc_STRVAR(set_doc,
931"set(iterable) --> set object\n\
932\n\
933Build an unordered collection.");
934
935PyTypeObject PySet_Type = {
936 PyObject_HEAD_INIT(&PyType_Type)
937 0, /* ob_size */
938 "set", /* tp_name */
939 sizeof(PySetObject), /* tp_basicsize */
940 0, /* tp_itemsize */
941 /* methods */
942 (destructor)set_dealloc, /* tp_dealloc */
943 (printfunc)set_tp_print, /* tp_print */
944 0, /* tp_getattr */
945 0, /* tp_setattr */
946 (cmpfunc)set_nocmp, /* tp_compare */
947 (reprfunc)set_repr, /* tp_repr */
948 &set_as_number, /* tp_as_number */
949 &set_as_sequence, /* tp_as_sequence */
950 0, /* tp_as_mapping */
951 set_nohash, /* tp_hash */
952 0, /* tp_call */
953 0, /* tp_str */
954 PyObject_GenericGetAttr, /* tp_getattro */
955 0, /* tp_setattro */
956 0, /* tp_as_buffer */
957 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES |
958 Py_TPFLAGS_BASETYPE, /* tp_flags */
959 set_doc, /* tp_doc */
960 (traverseproc)set_traverse, /* tp_traverse */
961 (inquiry)set_tp_clear, /* tp_clear */
962 (richcmpfunc)set_richcompare, /* tp_richcompare */
963 0, /* tp_weaklistoffset */
964 (getiterfunc)set_iter, /* tp_iter */
965 0, /* tp_iternext */
966 set_methods, /* tp_methods */
967 0, /* tp_members */
968 0, /* tp_getset */
969 0, /* tp_base */
970 0, /* tp_dict */
971 0, /* tp_descr_get */
972 0, /* tp_descr_set */
973 0, /* tp_dictoffset */
974 0, /* tp_init */
975 PyType_GenericAlloc, /* tp_alloc */
976 set_new, /* tp_new */
977 PyObject_GC_Del, /* tp_free */
978};
979
980/* frozenset object ********************************************************/
981
982
983static PyMethodDef frozenset_methods[] = {
984 {"copy", (PyCFunction)set_copy, METH_NOARGS,
985 copy_doc},
986 {"__copy__", (PyCFunction)set_copy, METH_NOARGS,
987 copy_doc},
988 {"difference",(PyCFunction)set_difference, METH_O,
989 difference_doc},
990 {"intersection",(PyCFunction)set_intersection, METH_O,
991 intersection_doc},
992 {"issubset",(PyCFunction)set_issubset, METH_O,
993 issubset_doc},
994 {"issuperset",(PyCFunction)set_issuperset, METH_O,
995 issuperset_doc},
996 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
997 reduce_doc},
998 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
999 symmetric_difference_doc},
1000 {"union", (PyCFunction)set_union, METH_O,
1001 union_doc},
1002 {NULL, NULL} /* sentinel */
1003};
1004
1005static PyNumberMethods frozenset_as_number = {
1006 0, /*nb_add*/
1007 (binaryfunc)set_sub, /*nb_subtract*/
1008 0, /*nb_multiply*/
1009 0, /*nb_divide*/
1010 0, /*nb_remainder*/
1011 0, /*nb_divmod*/
1012 0, /*nb_power*/
1013 0, /*nb_negative*/
1014 0, /*nb_positive*/
1015 0, /*nb_absolute*/
1016 0, /*nb_nonzero*/
1017 0, /*nb_invert*/
1018 0, /*nb_lshift*/
1019 0, /*nb_rshift*/
1020 (binaryfunc)set_and, /*nb_and*/
1021 (binaryfunc)set_xor, /*nb_xor*/
1022 (binaryfunc)set_or, /*nb_or*/
1023};
1024
1025PyDoc_STRVAR(frozenset_doc,
1026"frozenset(iterable) --> frozenset object\n\
1027\n\
1028Build an immutable unordered collection.");
1029
1030PyTypeObject PyFrozenSet_Type = {
1031 PyObject_HEAD_INIT(&PyType_Type)
1032 0, /* ob_size */
1033 "frozenset", /* tp_name */
1034 sizeof(PySetObject), /* tp_basicsize */
1035 0, /* tp_itemsize */
1036 /* methods */
1037 (destructor)set_dealloc, /* tp_dealloc */
1038 (printfunc)set_tp_print, /* tp_print */
1039 0, /* tp_getattr */
1040 0, /* tp_setattr */
1041 (cmpfunc)set_nocmp, /* tp_compare */
1042 (reprfunc)set_repr, /* tp_repr */
1043 &frozenset_as_number, /* tp_as_number */
1044 &set_as_sequence, /* tp_as_sequence */
1045 0, /* tp_as_mapping */
1046 frozenset_hash, /* tp_hash */
1047 0, /* tp_call */
1048 0, /* tp_str */
1049 PyObject_GenericGetAttr, /* tp_getattro */
1050 0, /* tp_setattro */
1051 0, /* tp_as_buffer */
1052 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES |
1053 Py_TPFLAGS_BASETYPE, /* tp_flags */
1054 frozenset_doc, /* tp_doc */
1055 (traverseproc)set_traverse, /* tp_traverse */
1056 0, /* tp_clear */
1057 (richcmpfunc)set_richcompare, /* tp_richcompare */
1058 0, /* tp_weaklistoffset */
1059 (getiterfunc)set_iter, /* tp_iter */
1060 0, /* tp_iternext */
1061 frozenset_methods, /* tp_methods */
1062 0, /* tp_members */
1063 0, /* tp_getset */
1064 0, /* tp_base */
1065 0, /* tp_dict */
1066 0, /* tp_descr_get */
1067 0, /* tp_descr_set */
1068 0, /* tp_dictoffset */
1069 0, /* tp_init */
1070 PyType_GenericAlloc, /* tp_alloc */
1071 set_new, /* tp_new */
1072 PyObject_GC_Del, /* tp_free */
1073};