blob: 2d77c7485a3b72f418c35b46bd81858299c25b4f [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 Hettinger19c2d772003-11-21 18:36:54 +0000107 PyObject *olddict;
108 PySetObject *tmp;
109 int result;
110
111 result = PySequence_Contains(so->data, key);
112 if (result == -1 && PyType_IsSubtype(key->ob_type, &PySet_Type)) {
113 PyErr_Clear();
114 tmp = (PySetObject *)make_new_set(&PyFrozenSet_Type, NULL);
115 if (tmp == NULL)
116 return -1;
117 olddict = tmp->data;
118 tmp->data = ((PySetObject *)(key))->data;
119 result = PySequence_Contains(so->data, (PyObject *)tmp);
120 tmp->data = olddict;
121 Py_DECREF(tmp);
122 }
123 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +0000124}
125
126static PyObject *
127set_copy(PySetObject *so)
128{
129 PyObject *data;
130 PySetObject *newso;
131
132 data = PyDict_Copy(so->data);
133 if (data == NULL)
134 return NULL;
135
136 newso = (PySetObject *)(so->ob_type->tp_alloc(so->ob_type, 0));
137 if (newso == NULL) {
138 Py_DECREF(data);
139 return NULL;
140 }
141 newso->data = data;
142 newso->hash = so->hash;
143 return (PyObject *)newso;
144}
145
146PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
147
148static PyObject *
149set_union(PySetObject *so, PyObject *other)
150{
151 PySetObject *result;
152 PyObject *item, *data, *it;
153
154 result = (PySetObject *)set_copy(so);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000155 if (result == NULL)
156 return NULL;
Raymond Hettinger82d73dd2003-11-20 22:54:33 +0000157
158 if (PyAnySet_Check(other)) {
159 if (PyDict_Merge(result->data, ((PySetObject *)other)->data, 0) == -1) {
160 Py_DECREF(result);
161 return NULL;
162 }
163 return (PyObject *)result;
164 }
165
Raymond Hettingera690a992003-11-16 16:17:49 +0000166 it = PyObject_GetIter(other);
167 if (it == NULL) {
168 Py_DECREF(result);
169 return NULL;
170 }
171 data = result->data;
172 while ((item = PyIter_Next(it)) != NULL) {
173 if (PyDict_SetItem(data, item, Py_True) == -1) {
174 Py_DECREF(it);
175 Py_DECREF(result);
176 Py_DECREF(item);
Raymond Hettingera690a992003-11-16 16:17:49 +0000177 return NULL;
178 }
179 Py_DECREF(item);
180 }
181 Py_DECREF(it);
182 if (PyErr_Occurred()) {
183 Py_DECREF(result);
184 return NULL;
185 }
186 return (PyObject *)result;
187}
188
189PyDoc_STRVAR(union_doc,
190 "Return the union of two sets as a new set.\n\
191\n\
192(i.e. all elements that are in either set.)");
193
194static PyObject *
195set_union_update(PySetObject *so, PyObject *other)
196{
197 PyObject *item, *data, *it;
198
Raymond Hettinger82d73dd2003-11-20 22:54:33 +0000199 if (PyAnySet_Check(other)) {
200 if (PyDict_Merge(so->data, ((PySetObject *)other)->data, 0) == -1)
201 return NULL;
202 Py_INCREF(so);
203 return (PyObject *)so;
204 }
205
Raymond Hettingera690a992003-11-16 16:17:49 +0000206 it = PyObject_GetIter(other);
207 if (it == NULL)
208 return NULL;
209 data = so->data;
210
211 while ((item = PyIter_Next(it)) != NULL) {
212 if (PyDict_SetItem(data, item, Py_True) == -1) {
213 Py_DECREF(it);
214 Py_DECREF(item);
Raymond Hettingera690a992003-11-16 16:17:49 +0000215 return NULL;
216 }
217 Py_DECREF(item);
218 }
219 Py_DECREF(it);
220 if (PyErr_Occurred())
221 return NULL;
222 Py_RETURN_NONE;
223}
224
225PyDoc_STRVAR(union_update_doc,
226"Update a set with the union of itself and another.");
227
228static PyObject *
229set_or(PySetObject *so, PyObject *other)
230{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000231 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000232 Py_INCREF(Py_NotImplemented);
233 return Py_NotImplemented;
234 }
235 return set_union(so, other);
236}
237
238static PyObject *
239set_ior(PySetObject *so, PyObject *other)
240{
241 PyObject *result;
242
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000243 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000244 Py_INCREF(Py_NotImplemented);
245 return Py_NotImplemented;
246 }
247 result = set_union_update(so, other);
248 if (result == NULL)
249 return NULL;
250 Py_DECREF(result);
251 Py_INCREF(so);
252 return (PyObject *)so;
253}
254
255static PyObject *
256set_intersection(PySetObject *so, PyObject *other)
257{
258 PySetObject *result;
259 PyObject *item, *selfdata, *tgtdata, *it;
260
261 result = (PySetObject *)make_new_set(so->ob_type, NULL);
262 if (result == NULL)
263 return NULL;
264
265 it = PyObject_GetIter(other);
266 if (it == NULL) {
267 Py_DECREF(result);
268 return NULL;
269 }
270
271 selfdata = so->data;
272 tgtdata = result->data;
273 while ((item = PyIter_Next(it)) != NULL) {
Raymond Hettinger1b92fd52003-11-18 14:15:31 +0000274 if (PySequence_Contains(selfdata, item)) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000275 if (PyDict_SetItem(tgtdata, item, Py_True) == -1) {
276 Py_DECREF(it);
277 Py_DECREF(result);
278 Py_DECREF(item);
Raymond Hettingera690a992003-11-16 16:17:49 +0000279 return NULL;
280 }
281 }
282 Py_DECREF(item);
283 }
284 Py_DECREF(it);
285 if (PyErr_Occurred()) {
286 Py_DECREF(result);
287 return NULL;
288 }
289 return (PyObject *)result;
290}
291
292PyDoc_STRVAR(intersection_doc,
293"Return the intersection of two sets as a new set.\n\
294\n\
295(i.e. all elements that are in both sets.)");
296
297static PyObject *
298set_intersection_update(PySetObject *so, PyObject *other)
299{
300 PyObject *item, *selfdata, *it, *newdict, *tmp;
301
302 newdict = PyDict_New();
303 if (newdict == NULL)
304 return newdict;
305
306 it = PyObject_GetIter(other);
307 if (it == NULL) {
308 Py_DECREF(newdict);
309 return NULL;
310 }
311
312 selfdata = so->data;
313 while ((item = PyIter_Next(it)) != NULL) {
Raymond Hettinger1b92fd52003-11-18 14:15:31 +0000314 if (PySequence_Contains(selfdata, item)) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000315 if (PyDict_SetItem(newdict, item, Py_True) == -1) {
316 Py_DECREF(newdict);
317 Py_DECREF(it);
318 Py_DECREF(item);
Raymond Hettingera690a992003-11-16 16:17:49 +0000319 return NULL;
320 }
321 }
322 Py_DECREF(item);
323 }
324 Py_DECREF(it);
325 if (PyErr_Occurred()) {
326 Py_DECREF(newdict);
327 return NULL;
328 }
329 tmp = so->data;
330 so->data = newdict;
331 Py_DECREF(tmp);
332 Py_RETURN_NONE;
333}
334
335PyDoc_STRVAR(intersection_update_doc,
336"Update a set with the intersection of itself and another.");
337
338static PyObject *
339set_and(PySetObject *so, PyObject *other)
340{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000341 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000342 Py_INCREF(Py_NotImplemented);
343 return Py_NotImplemented;
344 }
345 return set_intersection(so, other);
346}
347
348static PyObject *
349set_iand(PySetObject *so, PyObject *other)
350{
351 PyObject *result;
352
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000353 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000354 Py_INCREF(Py_NotImplemented);
355 return Py_NotImplemented;
356 }
357 result = set_intersection_update(so, other);
358 if (result == NULL)
359 return NULL;
360 Py_DECREF(result);
361 Py_INCREF(so);
362 return (PyObject *)so;
363}
364
365static PyObject *
366set_difference(PySetObject *so, PyObject *other)
367{
368 PySetObject *result;
369 PyObject *item, *tgtdata, *it;
370
371 result = (PySetObject *)set_copy(so);
372 if (result == NULL)
373 return NULL;
374
375 it = PyObject_GetIter(other);
376 if (it == NULL) {
377 Py_DECREF(result);
378 return NULL;
379 }
380
381 tgtdata = result->data;
382 while ((item = PyIter_Next(it)) != NULL) {
383 if (PyDict_DelItem(tgtdata, item) == -1) {
384 if (PyErr_ExceptionMatches(PyExc_KeyError))
385 PyErr_Clear();
386 else {
387 Py_DECREF(it);
388 Py_DECREF(result);
389 Py_DECREF(item);
390 return NULL;
391 }
392 }
393 Py_DECREF(item);
394 }
395 Py_DECREF(it);
396 if (PyErr_Occurred()) {
397 Py_DECREF(result);
398 return NULL;
399 }
400 return (PyObject *)result;
401}
402
403PyDoc_STRVAR(difference_doc,
404"Return the difference of two sets as a new set.\n\
405\n\
406(i.e. all elements that are in this set but not the other.)");
407
408static PyObject *
409set_difference_update(PySetObject *so, PyObject *other)
410{
411 PyObject *item, *tgtdata, *it;
412
413 it = PyObject_GetIter(other);
414 if (it == NULL)
415 return NULL;
416
417 tgtdata = so->data;
418 while ((item = PyIter_Next(it)) != NULL) {
419 if (PyDict_DelItem(tgtdata, item) == -1) {
420 if (PyErr_ExceptionMatches(PyExc_KeyError))
421 PyErr_Clear();
422 else {
423 Py_DECREF(it);
424 Py_DECREF(item);
425 return NULL;
426 }
427 }
428 Py_DECREF(item);
429 }
430 Py_DECREF(it);
431 if (PyErr_Occurred())
432 return NULL;
433 Py_RETURN_NONE;
434}
435
436PyDoc_STRVAR(difference_update_doc,
437"Remove all elements of another set from this set.");
438
439static PyObject *
440set_sub(PySetObject *so, PyObject *other)
441{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000442 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000443 Py_INCREF(Py_NotImplemented);
444 return Py_NotImplemented;
445 }
446 return set_difference(so, other);
447}
448
449static PyObject *
450set_isub(PySetObject *so, PyObject *other)
451{
452 PyObject *result;
453
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000454 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000455 Py_INCREF(Py_NotImplemented);
456 return Py_NotImplemented;
457 }
458 result = set_difference_update(so, other);
459 if (result == NULL)
460 return NULL;
461 Py_DECREF(result);
462 Py_INCREF(so);
463 return (PyObject *)so;
464}
465
466static PyObject *
467set_symmetric_difference(PySetObject *so, PyObject *other)
468{
Raymond Hettinger82d73dd2003-11-20 22:54:33 +0000469 PySetObject *result, *otherset=NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +0000470 PyObject *item, *selfdata, *otherdata, *tgtdata, *it;
471
472 selfdata = so->data;
473
474 result = (PySetObject *)set_copy(so);
475 if (result == NULL)
476 return NULL;
477 tgtdata = result->data;
478
Raymond Hettinger82d73dd2003-11-20 22:54:33 +0000479 if (PyDict_Check(other))
480 otherdata = other;
481 else if (PyAnySet_Check(other))
482 otherdata = ((PySetObject *)other)->data;
483 else {
484 otherset = (PySetObject *)make_new_set(so->ob_type, other);
485 if (otherset == NULL) {
486 Py_DECREF(result);
487 return NULL;
488 }
489 otherdata = otherset->data;
490 }
Raymond Hettingera690a992003-11-16 16:17:49 +0000491
492 it = PyObject_GetIter(otherdata);
493 if (it == NULL) {
Raymond Hettinger82d73dd2003-11-20 22:54:33 +0000494 Py_XDECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +0000495 Py_DECREF(result);
496 return NULL;
497 }
498
499 while ((item = PyIter_Next(it)) != NULL) {
500 if (PyDict_DelItem(tgtdata, item) == -1) {
501 PyErr_Clear();
502 if (PyDict_SetItem(tgtdata, item, Py_True) == -1) {
503 Py_DECREF(it);
Raymond Hettinger82d73dd2003-11-20 22:54:33 +0000504 Py_XDECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +0000505 Py_DECREF(result);
506 Py_DECREF(item);
Raymond Hettingera690a992003-11-16 16:17:49 +0000507 return NULL;
508 }
509 }
510 Py_DECREF(item);
511 }
512 Py_DECREF(it);
Raymond Hettinger82d73dd2003-11-20 22:54:33 +0000513 Py_XDECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +0000514 if (PyErr_Occurred()) {
515 Py_DECREF(result);
516 return NULL;
517 }
518 return (PyObject *)result;
519}
520
521PyDoc_STRVAR(symmetric_difference_doc,
522"Return the symmetric difference of two sets as a new set.\n\
523\n\
524(i.e. all elements that are in exactly one of the sets.)");
525
526static PyObject *
527set_symmetric_difference_update(PySetObject *so, PyObject *other)
528{
529 PyObject *item, *selfdata, *it, *otherdata;
530 PySetObject *otherset = NULL;
531
532 selfdata = so->data;
533
534 if (PyDict_Check(other))
535 otherdata = other;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000536 else if (PyAnySet_Check(other))
Raymond Hettingera690a992003-11-16 16:17:49 +0000537 otherdata = ((PySetObject *)other)->data;
538 else {
539 otherset = (PySetObject *)make_new_set(so->ob_type, other);
540 if (otherset == NULL)
541 return NULL;
542 otherdata = otherset->data;
543 }
544
545 it = PyObject_GetIter(otherdata);
546 if (it == NULL)
547 return NULL;
548
549 while ((item = PyIter_Next(it)) != NULL) {
Raymond Hettinger1b92fd52003-11-18 14:15:31 +0000550 if (PySequence_Contains(selfdata, item)) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000551 if (PyDict_DelItem(selfdata, item) == -1) {
552 Py_XDECREF(otherset);
553 Py_DECREF(it);
554 Py_DECREF(item);
Raymond Hettingera690a992003-11-16 16:17:49 +0000555 return NULL;
556 }
557 } else {
558 if (PyDict_SetItem(selfdata, item, Py_True) == -1) {
559 Py_XDECREF(otherset);
560 Py_DECREF(it);
561 Py_DECREF(item);
Raymond Hettingera690a992003-11-16 16:17:49 +0000562 return NULL;
563 }
564 }
565 Py_DECREF(item);
566 }
567 Py_XDECREF(otherset);
568 Py_DECREF(it);
569 if (PyErr_Occurred())
570 return NULL;
571 Py_RETURN_NONE;
572}
573
574PyDoc_STRVAR(symmetric_difference_update_doc,
575"Update a set with the symmetric difference of itself and another.");
576
577static PyObject *
578set_xor(PySetObject *so, PyObject *other)
579{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000580 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000581 Py_INCREF(Py_NotImplemented);
582 return Py_NotImplemented;
583 }
584 return set_symmetric_difference(so, other);
585}
586
587static PyObject *
588set_ixor(PySetObject *so, PyObject *other)
589{
590 PyObject *result;
591
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000592 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000593 Py_INCREF(Py_NotImplemented);
594 return Py_NotImplemented;
595 }
596 result = set_symmetric_difference_update(so, other);
597 if (result == NULL)
598 return NULL;
599 Py_DECREF(result);
600 Py_INCREF(so);
601 return (PyObject *)so;
602}
603
604static PyObject *
605set_issubset(PySetObject *so, PyObject *other)
606{
Raymond Hettinger3fbec702003-11-21 07:56:36 +0000607 PyObject *otherdata, *it, *item, *tmp, *result;
Raymond Hettingera690a992003-11-16 16:17:49 +0000608
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000609 if (!PyAnySet_Check(other)) {
Raymond Hettinger3fbec702003-11-21 07:56:36 +0000610 tmp = make_new_set(&PySet_Type, other);
611 if (tmp == NULL)
612 return NULL;
613 result = set_issubset(so, tmp);
614 Py_DECREF(tmp);
615 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +0000616 }
617 if (set_len(so) > set_len((PySetObject *)other))
618 Py_RETURN_FALSE;
619
620 it = PyObject_GetIter(so->data);
621 if (it == NULL)
622 return NULL;
623
624 otherdata = ((PySetObject *)other)->data;
625 while ((item = PyIter_Next(it)) != NULL) {
Raymond Hettinger1b92fd52003-11-18 14:15:31 +0000626 if (!PySequence_Contains(otherdata, item)) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000627 Py_DECREF(it);
628 Py_DECREF(item);
629 Py_RETURN_FALSE;
630 }
631 Py_DECREF(item);
632 }
633 Py_DECREF(it);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000634 if (PyErr_Occurred())
635 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +0000636 Py_RETURN_TRUE;
637}
638
639PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
640
641static PyObject *
642set_issuperset(PySetObject *so, PyObject *other)
643{
Raymond Hettinger3fbec702003-11-21 07:56:36 +0000644 PyObject *tmp, *result;
645
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000646 if (!PyAnySet_Check(other)) {
Raymond Hettinger3fbec702003-11-21 07:56:36 +0000647 tmp = make_new_set(&PySet_Type, other);
648 if (tmp == NULL)
649 return NULL;
650 result = set_issuperset(so, tmp);
651 Py_DECREF(tmp);
652 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +0000653 }
654 return set_issubset((PySetObject *)other, (PyObject *)so);
655}
656
657PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
658
659static long
660set_nohash(PyObject *self)
661{
662 PyErr_SetString(PyExc_TypeError, "set objects are unhashable");
663 return -1;
664}
665
666static int
667set_nocmp(PyObject *self)
668{
669 PyErr_SetString(PyExc_TypeError, "cannot compare sets using cmp()");
670 return -1;
671}
672
673static long
674frozenset_hash(PyObject *self)
675{
676 PyObject *it, *item;
677 PySetObject *so = (PySetObject *)self;
Raymond Hettinger82d73dd2003-11-20 22:54:33 +0000678 long hash = 0, x;
Raymond Hettingera690a992003-11-16 16:17:49 +0000679
680 if (so->hash != -1)
681 return so->hash;
682
683 it = PyObject_GetIter(((PySetObject *)so)->data);
684 if (it == NULL)
685 return -1;
686
687 while ((item = PyIter_Next(it)) != NULL) {
Raymond Hettinger82d73dd2003-11-20 22:54:33 +0000688 x = PyObject_Hash(item);
689 /* Applying x*(x+1) breaks-up linear relationships so that
690 h(1) ^ h(2) will be less likely to coincide with hash(3).
691 Multiplying by a large prime increases the dispersion
692 between consecutive hashes. Adding one bit from the
693 original restores the one bit lost during the multiply
694 (all the products are even numbers). */
695 hash ^= (x * (x+1) * 3644798167) | (x&1);
Raymond Hettingera690a992003-11-16 16:17:49 +0000696 Py_DECREF(item);
697 }
Raymond Hettingera690a992003-11-16 16:17:49 +0000698 Py_DECREF(it);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000699 if (PyErr_Occurred())
700 return -1;
701 so->hash = hash;
Raymond Hettingera690a992003-11-16 16:17:49 +0000702 return hash;
703}
704
705static PyObject *
706set_richcompare(PySetObject *v, PyObject *w, int op)
707{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000708 if(!PyAnySet_Check(w)) {
709 if (op == Py_EQ)
710 Py_RETURN_FALSE;
711 if (op == Py_NE)
712 Py_RETURN_TRUE;
Raymond Hettingera690a992003-11-16 16:17:49 +0000713 PyErr_SetString(PyExc_TypeError, "can only compare to a set");
714 return NULL;
715 }
716 switch (op) {
717 case Py_EQ:
718 case Py_NE:
719 return PyObject_RichCompare(((PySetObject *)v)->data,
720 ((PySetObject *)w)->data, op);
721 case Py_LE:
722 return set_issubset((PySetObject *)v, w);
723 case Py_GE:
724 return set_issuperset((PySetObject *)v, w);
725 case Py_LT:
726 if (set_len(v) >= set_len((PySetObject *)w))
727 Py_RETURN_FALSE;
728 return set_issubset((PySetObject *)v, w);
729 case Py_GT:
730 if (set_len(v) <= set_len((PySetObject *)w))
731 Py_RETURN_FALSE;
732 return set_issuperset((PySetObject *)v, w);
733 }
734 Py_INCREF(Py_NotImplemented);
735 return Py_NotImplemented;
736}
737
738static PyObject *
739set_repr(PySetObject *so)
740{
741 PyObject *keys, *result, *listrepr;
742
743 keys = PyDict_Keys(so->data);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000744 if (keys == NULL)
745 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +0000746 listrepr = PyObject_Repr(keys);
747 Py_DECREF(keys);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000748 if (listrepr == NULL)
749 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +0000750
751 result = PyString_FromFormat("%s(%s)", so->ob_type->tp_name,
752 PyString_AS_STRING(listrepr));
753 Py_DECREF(listrepr);
754 return result;
755}
756
757static int
758set_tp_print(PySetObject *so, FILE *fp, int flags)
759{
760 PyObject *it, *item;
761 int firstpass=1;
762
763 it = PyObject_GetIter(so->data);
764 if (it == NULL)
765 return -1;
766 fprintf(fp, "%s([", so->ob_type->tp_name);
767
768 while ((item = PyIter_Next(it)) != NULL) {
769 if (firstpass == 1)
770 firstpass = 0;
771 else
Raymond Hettingere2c277a2003-11-16 16:36:58 +0000772 fprintf(fp, ", ");
Raymond Hettingera690a992003-11-16 16:17:49 +0000773 if (PyObject_Print(item, fp, 0) != 0) {
774 Py_DECREF(it);
775 Py_DECREF(item);
776 return -1;
777 }
778 Py_DECREF(item);
779 }
780 Py_DECREF(it);
781 fprintf(fp, "])");
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000782 if (PyErr_Occurred())
783 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000784 return 0;
785}
786
787static PyObject *
788set_clear(PySetObject *so)
789{
790 PyDict_Clear(so->data);
791 so->hash = -1;
792 Py_INCREF(Py_None);
793 return Py_None;
794}
795
796PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
797
798static int
799set_tp_clear(PySetObject *so)
800{
801 PyDict_Clear(so->data);
802 so->hash = -1;
803 return 0;
804}
805
806static PyObject *
807set_add(PySetObject *so, PyObject *item)
808{
809 if (PyDict_SetItem(so->data, item, Py_True) == -1)
810 return NULL;
811 Py_INCREF(Py_None);
812 return Py_None;
813}
814
815PyDoc_STRVAR(add_doc,
816"Add an element to a set.\n\
817\n\
818This has no effect if the element is already present.");
819
820static PyObject *
821set_remove(PySetObject *so, PyObject *item)
822{
823 if (PyDict_DelItem(so->data, item) == -1)
824 return NULL;
825 Py_INCREF(Py_None);
826 return Py_None;
827}
828
829PyDoc_STRVAR(remove_doc,
830"Remove an element from a set; it must be a member.\n\
831\n\
832If the element is not a member, raise a KeyError.");
833
834static PyObject *
835set_discard(PySetObject *so, PyObject *item)
836{
Guido van Rossumb61982b2003-11-18 19:27:19 +0000837 if (PyDict_DelItem(so->data, item) == -1) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000838 if (PyErr_ExceptionMatches(PyExc_KeyError))
839 PyErr_Clear();
840 else
841 return NULL;
Guido van Rossumb61982b2003-11-18 19:27:19 +0000842 }
Raymond Hettingera690a992003-11-16 16:17:49 +0000843 Py_INCREF(Py_None);
844 return Py_None;
845}
846
847PyDoc_STRVAR(discard_doc,
848"Remove an element from a set if it is a member.\n\
849\n\
850If the element is not a member, do nothing.");
851
852static PyObject *
853set_pop(PySetObject *so)
854{
855 PyObject *key, *value;
856 int pos = 0;
857
858 if (!PyDict_Next(so->data, &pos, &key, &value)) {
859 PyErr_SetString(PyExc_KeyError, "pop from an empty set");
860 return NULL;
861 }
862 Py_INCREF(key);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000863 if (PyDict_DelItem(so->data, key) == -1) {
864 Py_DECREF(key);
865 return NULL;
866 }
Raymond Hettingera690a992003-11-16 16:17:49 +0000867 return key;
868}
869
870PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.");
871
872static PyObject *
873set_reduce(PySetObject *so)
874{
875 PyObject *keys=NULL, *args=NULL, *result=NULL;
876
877 keys = PyDict_Keys(so->data);
878 if (keys == NULL)
879 goto done;
880 args = PyTuple_Pack(1, keys);
881 if (args == NULL)
882 goto done;
883 result = PyTuple_Pack(2, so->ob_type, args);
884done:
885 Py_XDECREF(args);
886 Py_XDECREF(keys);
887 return result;
888}
889
890PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
891
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000892static int
893set_init(PySetObject *self, PyObject *args, PyObject *kwds)
894{
895 PyObject *iterable = NULL;
896 PyObject *result;
897
898 if (!PyAnySet_Check(self))
899 return -1;
900 if (!PyArg_UnpackTuple(args, self->ob_type->tp_name, 0, 1, &iterable))
901 return -1;
902 PyDict_Clear(self->data);
903 self->hash = -1;
904 if (iterable == NULL)
905 return 0;
906 result = set_union_update(self, iterable);
907 if (result != NULL) {
908 Py_DECREF(result);
909 return 0;
910 }
911 return -1;
912}
913
Raymond Hettingera690a992003-11-16 16:17:49 +0000914static PySequenceMethods set_as_sequence = {
915 (inquiry)set_len, /* sq_length */
916 0, /* sq_concat */
917 0, /* sq_repeat */
918 0, /* sq_item */
919 0, /* sq_slice */
920 0, /* sq_ass_item */
921 0, /* sq_ass_slice */
922 (objobjproc)set_contains, /* sq_contains */
923};
924
925/* set object ********************************************************/
926
927static PyMethodDef set_methods[] = {
928 {"add", (PyCFunction)set_add, METH_O,
929 add_doc},
930 {"clear", (PyCFunction)set_clear, METH_NOARGS,
931 clear_doc},
932 {"copy", (PyCFunction)set_copy, METH_NOARGS,
933 copy_doc},
934 {"__copy__", (PyCFunction)set_copy, METH_NOARGS,
935 copy_doc},
936 {"discard", (PyCFunction)set_discard, METH_O,
937 discard_doc},
938 {"difference", (PyCFunction)set_difference, METH_O,
939 difference_doc},
940 {"difference_update", (PyCFunction)set_difference_update, METH_O,
941 difference_update_doc},
942 {"intersection",(PyCFunction)set_intersection, METH_O,
943 intersection_doc},
944 {"intersection_update",(PyCFunction)set_intersection_update, METH_O,
945 intersection_update_doc},
946 {"issubset", (PyCFunction)set_issubset, METH_O,
947 issubset_doc},
948 {"issuperset", (PyCFunction)set_issuperset, METH_O,
949 issuperset_doc},
950 {"pop", (PyCFunction)set_pop, METH_NOARGS,
951 pop_doc},
952 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
953 reduce_doc},
954 {"remove", (PyCFunction)set_remove, METH_O,
955 remove_doc},
956 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
957 symmetric_difference_doc},
958 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
959 symmetric_difference_update_doc},
960 {"union", (PyCFunction)set_union, METH_O,
961 union_doc},
962 {"union_update",(PyCFunction)set_union_update, METH_O,
963 union_update_doc},
964 {NULL, NULL} /* sentinel */
965};
966
967static PyNumberMethods set_as_number = {
968 0, /*nb_add*/
969 (binaryfunc)set_sub, /*nb_subtract*/
970 0, /*nb_multiply*/
971 0, /*nb_divide*/
972 0, /*nb_remainder*/
973 0, /*nb_divmod*/
974 0, /*nb_power*/
975 0, /*nb_negative*/
976 0, /*nb_positive*/
977 0, /*nb_absolute*/
978 0, /*nb_nonzero*/
979 0, /*nb_invert*/
980 0, /*nb_lshift*/
981 0, /*nb_rshift*/
982 (binaryfunc)set_and, /*nb_and*/
983 (binaryfunc)set_xor, /*nb_xor*/
984 (binaryfunc)set_or, /*nb_or*/
985 0, /*nb_coerce*/
986 0, /*nb_int*/
987 0, /*nb_long*/
988 0, /*nb_float*/
989 0, /*nb_oct*/
990 0, /*nb_hex*/
991 0, /*nb_inplace_add*/
992 (binaryfunc)set_isub, /*nb_inplace_subtract*/
993 0, /*nb_inplace_multiply*/
994 0, /*nb_inplace_divide*/
995 0, /*nb_inplace_remainder*/
996 0, /*nb_inplace_power*/
997 0, /*nb_inplace_lshift*/
998 0, /*nb_inplace_rshift*/
999 (binaryfunc)set_iand, /*nb_inplace_and*/
1000 (binaryfunc)set_ixor, /*nb_inplace_xor*/
1001 (binaryfunc)set_ior, /*nb_inplace_or*/
1002};
1003
1004PyDoc_STRVAR(set_doc,
1005"set(iterable) --> set object\n\
1006\n\
1007Build an unordered collection.");
1008
1009PyTypeObject PySet_Type = {
1010 PyObject_HEAD_INIT(&PyType_Type)
1011 0, /* ob_size */
1012 "set", /* tp_name */
1013 sizeof(PySetObject), /* tp_basicsize */
1014 0, /* tp_itemsize */
1015 /* methods */
1016 (destructor)set_dealloc, /* tp_dealloc */
1017 (printfunc)set_tp_print, /* tp_print */
1018 0, /* tp_getattr */
1019 0, /* tp_setattr */
1020 (cmpfunc)set_nocmp, /* tp_compare */
1021 (reprfunc)set_repr, /* tp_repr */
1022 &set_as_number, /* tp_as_number */
1023 &set_as_sequence, /* tp_as_sequence */
1024 0, /* tp_as_mapping */
1025 set_nohash, /* tp_hash */
1026 0, /* tp_call */
1027 0, /* tp_str */
1028 PyObject_GenericGetAttr, /* tp_getattro */
1029 0, /* tp_setattro */
1030 0, /* tp_as_buffer */
1031 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES |
1032 Py_TPFLAGS_BASETYPE, /* tp_flags */
1033 set_doc, /* tp_doc */
1034 (traverseproc)set_traverse, /* tp_traverse */
1035 (inquiry)set_tp_clear, /* tp_clear */
1036 (richcmpfunc)set_richcompare, /* tp_richcompare */
1037 0, /* tp_weaklistoffset */
1038 (getiterfunc)set_iter, /* tp_iter */
1039 0, /* tp_iternext */
1040 set_methods, /* tp_methods */
1041 0, /* tp_members */
1042 0, /* tp_getset */
1043 0, /* tp_base */
1044 0, /* tp_dict */
1045 0, /* tp_descr_get */
1046 0, /* tp_descr_set */
1047 0, /* tp_dictoffset */
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001048 (initproc)set_init, /* tp_init */
Raymond Hettingera690a992003-11-16 16:17:49 +00001049 PyType_GenericAlloc, /* tp_alloc */
1050 set_new, /* tp_new */
1051 PyObject_GC_Del, /* tp_free */
1052};
1053
1054/* frozenset object ********************************************************/
1055
1056
1057static PyMethodDef frozenset_methods[] = {
1058 {"copy", (PyCFunction)set_copy, METH_NOARGS,
1059 copy_doc},
1060 {"__copy__", (PyCFunction)set_copy, METH_NOARGS,
1061 copy_doc},
1062 {"difference",(PyCFunction)set_difference, METH_O,
1063 difference_doc},
1064 {"intersection",(PyCFunction)set_intersection, METH_O,
1065 intersection_doc},
1066 {"issubset",(PyCFunction)set_issubset, METH_O,
1067 issubset_doc},
1068 {"issuperset",(PyCFunction)set_issuperset, METH_O,
1069 issuperset_doc},
1070 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
1071 reduce_doc},
1072 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
1073 symmetric_difference_doc},
1074 {"union", (PyCFunction)set_union, METH_O,
1075 union_doc},
1076 {NULL, NULL} /* sentinel */
1077};
1078
1079static PyNumberMethods frozenset_as_number = {
1080 0, /*nb_add*/
1081 (binaryfunc)set_sub, /*nb_subtract*/
1082 0, /*nb_multiply*/
1083 0, /*nb_divide*/
1084 0, /*nb_remainder*/
1085 0, /*nb_divmod*/
1086 0, /*nb_power*/
1087 0, /*nb_negative*/
1088 0, /*nb_positive*/
1089 0, /*nb_absolute*/
1090 0, /*nb_nonzero*/
1091 0, /*nb_invert*/
1092 0, /*nb_lshift*/
1093 0, /*nb_rshift*/
1094 (binaryfunc)set_and, /*nb_and*/
1095 (binaryfunc)set_xor, /*nb_xor*/
1096 (binaryfunc)set_or, /*nb_or*/
1097};
1098
1099PyDoc_STRVAR(frozenset_doc,
1100"frozenset(iterable) --> frozenset object\n\
1101\n\
1102Build an immutable unordered collection.");
1103
1104PyTypeObject PyFrozenSet_Type = {
1105 PyObject_HEAD_INIT(&PyType_Type)
1106 0, /* ob_size */
1107 "frozenset", /* tp_name */
1108 sizeof(PySetObject), /* tp_basicsize */
1109 0, /* tp_itemsize */
1110 /* methods */
1111 (destructor)set_dealloc, /* tp_dealloc */
1112 (printfunc)set_tp_print, /* tp_print */
1113 0, /* tp_getattr */
1114 0, /* tp_setattr */
1115 (cmpfunc)set_nocmp, /* tp_compare */
1116 (reprfunc)set_repr, /* tp_repr */
1117 &frozenset_as_number, /* tp_as_number */
1118 &set_as_sequence, /* tp_as_sequence */
1119 0, /* tp_as_mapping */
1120 frozenset_hash, /* tp_hash */
1121 0, /* tp_call */
1122 0, /* tp_str */
1123 PyObject_GenericGetAttr, /* tp_getattro */
1124 0, /* tp_setattro */
1125 0, /* tp_as_buffer */
1126 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES |
1127 Py_TPFLAGS_BASETYPE, /* tp_flags */
1128 frozenset_doc, /* tp_doc */
1129 (traverseproc)set_traverse, /* tp_traverse */
1130 0, /* tp_clear */
1131 (richcmpfunc)set_richcompare, /* tp_richcompare */
1132 0, /* tp_weaklistoffset */
1133 (getiterfunc)set_iter, /* tp_iter */
1134 0, /* tp_iternext */
1135 frozenset_methods, /* tp_methods */
1136 0, /* tp_members */
1137 0, /* tp_getset */
1138 0, /* tp_base */
1139 0, /* tp_dict */
1140 0, /* tp_descr_get */
1141 0, /* tp_descr_set */
1142 0, /* tp_dictoffset */
1143 0, /* tp_init */
1144 PyType_GenericAlloc, /* tp_alloc */
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001145 frozenset_new, /* tp_new */
Raymond Hettingera690a992003-11-16 16:17:49 +00001146 PyObject_GC_Del, /* tp_free */
1147};