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