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