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