blob: be73954b45466a1b4050d3a1b8a40e4ae20f7f42 [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 Hettingerbfd334a2003-11-22 03:55:23 +000076static PyObject *
77frozenset_dict_wrapper(PyObject *d)
78{
79 PySetObject *w;
80
81 assert(PyDict_Check(d));
82 w = (PySetObject *)make_new_set(&PyFrozenSet_Type, NULL);
83 if (w == NULL)
84 return NULL;
85 Py_DECREF(w->data);
86 Py_INCREF(d);
87 w->data = d;
88 return (PyObject *)w;
89}
90
Raymond Hettingera690a992003-11-16 16:17:49 +000091static void
92set_dealloc(PySetObject *so)
93{
94 PyObject_GC_UnTrack(so);
95 Py_XDECREF(so->data);
96 so->ob_type->tp_free(so);
97}
98
99static int
100set_traverse(PySetObject *so, visitproc visit, void *arg)
101{
102 if (so->data)
103 return visit(so->data, arg);
104 return 0;
105}
106
107static PyObject *
108set_iter(PySetObject *so)
109{
110 return PyObject_GetIter(so->data);
111}
112
113static int
114set_len(PySetObject *so)
115{
116 return PyDict_Size(so->data);
117}
118
119static int
120set_contains(PySetObject *so, PyObject *key)
121{
Raymond Hettingerbfd334a2003-11-22 03:55:23 +0000122 PyObject *tmp;
Raymond Hettinger19c2d772003-11-21 18:36:54 +0000123 int result;
124
125 result = PySequence_Contains(so->data, key);
126 if (result == -1 && PyType_IsSubtype(key->ob_type, &PySet_Type)) {
127 PyErr_Clear();
Raymond Hettingerbfd334a2003-11-22 03:55:23 +0000128 tmp = frozenset_dict_wrapper(((PySetObject *)(key))->data);
Raymond Hettinger19c2d772003-11-21 18:36:54 +0000129 if (tmp == NULL)
130 return -1;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +0000131 result = PySequence_Contains(so->data, tmp);
Raymond Hettinger19c2d772003-11-21 18:36:54 +0000132 Py_DECREF(tmp);
133 }
134 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +0000135}
136
137static PyObject *
138set_copy(PySetObject *so)
139{
140 PyObject *data;
141 PySetObject *newso;
142
143 data = PyDict_Copy(so->data);
144 if (data == NULL)
145 return NULL;
146
147 newso = (PySetObject *)(so->ob_type->tp_alloc(so->ob_type, 0));
148 if (newso == NULL) {
149 Py_DECREF(data);
150 return NULL;
151 }
152 newso->data = data;
153 newso->hash = so->hash;
154 return (PyObject *)newso;
155}
156
157PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
158
159static PyObject *
160set_union(PySetObject *so, PyObject *other)
161{
162 PySetObject *result;
163 PyObject *item, *data, *it;
164
165 result = (PySetObject *)set_copy(so);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000166 if (result == NULL)
167 return NULL;
Raymond Hettinger82d73dd2003-11-20 22:54:33 +0000168
169 if (PyAnySet_Check(other)) {
170 if (PyDict_Merge(result->data, ((PySetObject *)other)->data, 0) == -1) {
171 Py_DECREF(result);
172 return NULL;
173 }
174 return (PyObject *)result;
175 }
176
Raymond Hettingera690a992003-11-16 16:17:49 +0000177 it = PyObject_GetIter(other);
178 if (it == NULL) {
179 Py_DECREF(result);
180 return NULL;
181 }
182 data = result->data;
183 while ((item = PyIter_Next(it)) != NULL) {
184 if (PyDict_SetItem(data, item, Py_True) == -1) {
185 Py_DECREF(it);
186 Py_DECREF(result);
187 Py_DECREF(item);
Raymond Hettingera690a992003-11-16 16:17:49 +0000188 return NULL;
189 }
190 Py_DECREF(item);
191 }
192 Py_DECREF(it);
193 if (PyErr_Occurred()) {
194 Py_DECREF(result);
195 return NULL;
196 }
197 return (PyObject *)result;
198}
199
200PyDoc_STRVAR(union_doc,
201 "Return the union of two sets as a new set.\n\
202\n\
203(i.e. all elements that are in either set.)");
204
205static PyObject *
206set_union_update(PySetObject *so, PyObject *other)
207{
208 PyObject *item, *data, *it;
209
Raymond Hettinger82d73dd2003-11-20 22:54:33 +0000210 if (PyAnySet_Check(other)) {
211 if (PyDict_Merge(so->data, ((PySetObject *)other)->data, 0) == -1)
212 return NULL;
213 Py_INCREF(so);
214 return (PyObject *)so;
215 }
216
Raymond Hettingera690a992003-11-16 16:17:49 +0000217 it = PyObject_GetIter(other);
218 if (it == NULL)
219 return NULL;
220 data = so->data;
221
222 while ((item = PyIter_Next(it)) != NULL) {
223 if (PyDict_SetItem(data, item, Py_True) == -1) {
224 Py_DECREF(it);
225 Py_DECREF(item);
Raymond Hettingera690a992003-11-16 16:17:49 +0000226 return NULL;
227 }
228 Py_DECREF(item);
229 }
230 Py_DECREF(it);
231 if (PyErr_Occurred())
232 return NULL;
233 Py_RETURN_NONE;
234}
235
236PyDoc_STRVAR(union_update_doc,
237"Update a set with the union of itself and another.");
238
239static PyObject *
240set_or(PySetObject *so, PyObject *other)
241{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000242 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000243 Py_INCREF(Py_NotImplemented);
244 return Py_NotImplemented;
245 }
246 return set_union(so, other);
247}
248
249static PyObject *
250set_ior(PySetObject *so, PyObject *other)
251{
252 PyObject *result;
253
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000254 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000255 Py_INCREF(Py_NotImplemented);
256 return Py_NotImplemented;
257 }
258 result = set_union_update(so, other);
259 if (result == NULL)
260 return NULL;
261 Py_DECREF(result);
262 Py_INCREF(so);
263 return (PyObject *)so;
264}
265
266static PyObject *
267set_intersection(PySetObject *so, PyObject *other)
268{
269 PySetObject *result;
270 PyObject *item, *selfdata, *tgtdata, *it;
271
272 result = (PySetObject *)make_new_set(so->ob_type, NULL);
273 if (result == NULL)
274 return NULL;
275
276 it = PyObject_GetIter(other);
277 if (it == NULL) {
278 Py_DECREF(result);
279 return NULL;
280 }
281
282 selfdata = so->data;
283 tgtdata = result->data;
284 while ((item = PyIter_Next(it)) != NULL) {
Raymond Hettinger1b92fd52003-11-18 14:15:31 +0000285 if (PySequence_Contains(selfdata, item)) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000286 if (PyDict_SetItem(tgtdata, item, Py_True) == -1) {
287 Py_DECREF(it);
288 Py_DECREF(result);
289 Py_DECREF(item);
Raymond Hettingera690a992003-11-16 16:17:49 +0000290 return NULL;
291 }
292 }
293 Py_DECREF(item);
294 }
295 Py_DECREF(it);
296 if (PyErr_Occurred()) {
297 Py_DECREF(result);
298 return NULL;
299 }
300 return (PyObject *)result;
301}
302
303PyDoc_STRVAR(intersection_doc,
304"Return the intersection of two sets as a new set.\n\
305\n\
306(i.e. all elements that are in both sets.)");
307
308static PyObject *
309set_intersection_update(PySetObject *so, PyObject *other)
310{
311 PyObject *item, *selfdata, *it, *newdict, *tmp;
312
313 newdict = PyDict_New();
314 if (newdict == NULL)
315 return newdict;
316
317 it = PyObject_GetIter(other);
318 if (it == NULL) {
319 Py_DECREF(newdict);
320 return NULL;
321 }
322
323 selfdata = so->data;
324 while ((item = PyIter_Next(it)) != NULL) {
Raymond Hettinger1b92fd52003-11-18 14:15:31 +0000325 if (PySequence_Contains(selfdata, item)) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000326 if (PyDict_SetItem(newdict, item, Py_True) == -1) {
327 Py_DECREF(newdict);
328 Py_DECREF(it);
329 Py_DECREF(item);
Raymond Hettingera690a992003-11-16 16:17:49 +0000330 return NULL;
331 }
332 }
333 Py_DECREF(item);
334 }
335 Py_DECREF(it);
336 if (PyErr_Occurred()) {
337 Py_DECREF(newdict);
338 return NULL;
339 }
340 tmp = so->data;
341 so->data = newdict;
342 Py_DECREF(tmp);
343 Py_RETURN_NONE;
344}
345
346PyDoc_STRVAR(intersection_update_doc,
347"Update a set with the intersection of itself and another.");
348
349static PyObject *
350set_and(PySetObject *so, PyObject *other)
351{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000352 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000353 Py_INCREF(Py_NotImplemented);
354 return Py_NotImplemented;
355 }
356 return set_intersection(so, other);
357}
358
359static PyObject *
360set_iand(PySetObject *so, PyObject *other)
361{
362 PyObject *result;
363
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000364 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000365 Py_INCREF(Py_NotImplemented);
366 return Py_NotImplemented;
367 }
368 result = set_intersection_update(so, other);
369 if (result == NULL)
370 return NULL;
371 Py_DECREF(result);
372 Py_INCREF(so);
373 return (PyObject *)so;
374}
375
376static PyObject *
377set_difference(PySetObject *so, PyObject *other)
378{
379 PySetObject *result;
380 PyObject *item, *tgtdata, *it;
381
382 result = (PySetObject *)set_copy(so);
383 if (result == NULL)
384 return NULL;
385
386 it = PyObject_GetIter(other);
387 if (it == NULL) {
388 Py_DECREF(result);
389 return NULL;
390 }
391
392 tgtdata = result->data;
393 while ((item = PyIter_Next(it)) != NULL) {
394 if (PyDict_DelItem(tgtdata, item) == -1) {
395 if (PyErr_ExceptionMatches(PyExc_KeyError))
396 PyErr_Clear();
397 else {
398 Py_DECREF(it);
399 Py_DECREF(result);
400 Py_DECREF(item);
401 return NULL;
402 }
403 }
404 Py_DECREF(item);
405 }
406 Py_DECREF(it);
407 if (PyErr_Occurred()) {
408 Py_DECREF(result);
409 return NULL;
410 }
411 return (PyObject *)result;
412}
413
414PyDoc_STRVAR(difference_doc,
415"Return the difference of two sets as a new set.\n\
416\n\
417(i.e. all elements that are in this set but not the other.)");
418
419static PyObject *
420set_difference_update(PySetObject *so, PyObject *other)
421{
422 PyObject *item, *tgtdata, *it;
423
424 it = PyObject_GetIter(other);
425 if (it == NULL)
426 return NULL;
427
428 tgtdata = so->data;
429 while ((item = PyIter_Next(it)) != NULL) {
430 if (PyDict_DelItem(tgtdata, item) == -1) {
431 if (PyErr_ExceptionMatches(PyExc_KeyError))
432 PyErr_Clear();
433 else {
434 Py_DECREF(it);
435 Py_DECREF(item);
436 return NULL;
437 }
438 }
439 Py_DECREF(item);
440 }
441 Py_DECREF(it);
442 if (PyErr_Occurred())
443 return NULL;
444 Py_RETURN_NONE;
445}
446
447PyDoc_STRVAR(difference_update_doc,
448"Remove all elements of another set from this set.");
449
450static PyObject *
451set_sub(PySetObject *so, PyObject *other)
452{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000453 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000454 Py_INCREF(Py_NotImplemented);
455 return Py_NotImplemented;
456 }
457 return set_difference(so, other);
458}
459
460static PyObject *
461set_isub(PySetObject *so, PyObject *other)
462{
463 PyObject *result;
464
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000465 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000466 Py_INCREF(Py_NotImplemented);
467 return Py_NotImplemented;
468 }
469 result = set_difference_update(so, other);
470 if (result == NULL)
471 return NULL;
472 Py_DECREF(result);
473 Py_INCREF(so);
474 return (PyObject *)so;
475}
476
477static PyObject *
478set_symmetric_difference(PySetObject *so, PyObject *other)
479{
Raymond Hettinger82d73dd2003-11-20 22:54:33 +0000480 PySetObject *result, *otherset=NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +0000481 PyObject *item, *selfdata, *otherdata, *tgtdata, *it;
482
483 selfdata = so->data;
484
485 result = (PySetObject *)set_copy(so);
486 if (result == NULL)
487 return NULL;
488 tgtdata = result->data;
489
Raymond Hettinger82d73dd2003-11-20 22:54:33 +0000490 if (PyDict_Check(other))
491 otherdata = other;
492 else if (PyAnySet_Check(other))
493 otherdata = ((PySetObject *)other)->data;
494 else {
495 otherset = (PySetObject *)make_new_set(so->ob_type, other);
496 if (otherset == NULL) {
497 Py_DECREF(result);
498 return NULL;
499 }
500 otherdata = otherset->data;
501 }
Raymond Hettingera690a992003-11-16 16:17:49 +0000502
503 it = PyObject_GetIter(otherdata);
504 if (it == NULL) {
Raymond Hettinger82d73dd2003-11-20 22:54:33 +0000505 Py_XDECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +0000506 Py_DECREF(result);
507 return NULL;
508 }
509
510 while ((item = PyIter_Next(it)) != NULL) {
511 if (PyDict_DelItem(tgtdata, item) == -1) {
512 PyErr_Clear();
513 if (PyDict_SetItem(tgtdata, item, Py_True) == -1) {
514 Py_DECREF(it);
Raymond Hettinger82d73dd2003-11-20 22:54:33 +0000515 Py_XDECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +0000516 Py_DECREF(result);
517 Py_DECREF(item);
Raymond Hettingera690a992003-11-16 16:17:49 +0000518 return NULL;
519 }
520 }
521 Py_DECREF(item);
522 }
523 Py_DECREF(it);
Raymond Hettinger82d73dd2003-11-20 22:54:33 +0000524 Py_XDECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +0000525 if (PyErr_Occurred()) {
526 Py_DECREF(result);
527 return NULL;
528 }
529 return (PyObject *)result;
530}
531
532PyDoc_STRVAR(symmetric_difference_doc,
533"Return the symmetric difference of two sets as a new set.\n\
534\n\
535(i.e. all elements that are in exactly one of the sets.)");
536
537static PyObject *
538set_symmetric_difference_update(PySetObject *so, PyObject *other)
539{
540 PyObject *item, *selfdata, *it, *otherdata;
541 PySetObject *otherset = NULL;
542
543 selfdata = so->data;
544
545 if (PyDict_Check(other))
546 otherdata = other;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000547 else if (PyAnySet_Check(other))
Raymond Hettingera690a992003-11-16 16:17:49 +0000548 otherdata = ((PySetObject *)other)->data;
549 else {
550 otherset = (PySetObject *)make_new_set(so->ob_type, other);
551 if (otherset == NULL)
552 return NULL;
553 otherdata = otherset->data;
554 }
555
556 it = PyObject_GetIter(otherdata);
557 if (it == NULL)
558 return NULL;
559
560 while ((item = PyIter_Next(it)) != NULL) {
Raymond Hettinger1b92fd52003-11-18 14:15:31 +0000561 if (PySequence_Contains(selfdata, item)) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000562 if (PyDict_DelItem(selfdata, item) == -1) {
563 Py_XDECREF(otherset);
564 Py_DECREF(it);
565 Py_DECREF(item);
Raymond Hettingera690a992003-11-16 16:17:49 +0000566 return NULL;
567 }
568 } else {
569 if (PyDict_SetItem(selfdata, item, Py_True) == -1) {
570 Py_XDECREF(otherset);
571 Py_DECREF(it);
572 Py_DECREF(item);
Raymond Hettingera690a992003-11-16 16:17:49 +0000573 return NULL;
574 }
575 }
576 Py_DECREF(item);
577 }
578 Py_XDECREF(otherset);
579 Py_DECREF(it);
580 if (PyErr_Occurred())
581 return NULL;
582 Py_RETURN_NONE;
583}
584
585PyDoc_STRVAR(symmetric_difference_update_doc,
586"Update a set with the symmetric difference of itself and another.");
587
588static PyObject *
589set_xor(PySetObject *so, PyObject *other)
590{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000591 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000592 Py_INCREF(Py_NotImplemented);
593 return Py_NotImplemented;
594 }
595 return set_symmetric_difference(so, other);
596}
597
598static PyObject *
599set_ixor(PySetObject *so, PyObject *other)
600{
601 PyObject *result;
602
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000603 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000604 Py_INCREF(Py_NotImplemented);
605 return Py_NotImplemented;
606 }
607 result = set_symmetric_difference_update(so, other);
608 if (result == NULL)
609 return NULL;
610 Py_DECREF(result);
611 Py_INCREF(so);
612 return (PyObject *)so;
613}
614
615static PyObject *
616set_issubset(PySetObject *so, PyObject *other)
617{
Raymond Hettinger3fbec702003-11-21 07:56:36 +0000618 PyObject *otherdata, *it, *item, *tmp, *result;
Raymond Hettingera690a992003-11-16 16:17:49 +0000619
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000620 if (!PyAnySet_Check(other)) {
Raymond Hettinger3fbec702003-11-21 07:56:36 +0000621 tmp = make_new_set(&PySet_Type, other);
622 if (tmp == NULL)
623 return NULL;
624 result = set_issubset(so, tmp);
625 Py_DECREF(tmp);
626 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +0000627 }
628 if (set_len(so) > set_len((PySetObject *)other))
629 Py_RETURN_FALSE;
630
631 it = PyObject_GetIter(so->data);
632 if (it == NULL)
633 return NULL;
634
635 otherdata = ((PySetObject *)other)->data;
636 while ((item = PyIter_Next(it)) != NULL) {
Raymond Hettinger1b92fd52003-11-18 14:15:31 +0000637 if (!PySequence_Contains(otherdata, item)) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000638 Py_DECREF(it);
639 Py_DECREF(item);
640 Py_RETURN_FALSE;
641 }
642 Py_DECREF(item);
643 }
644 Py_DECREF(it);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000645 if (PyErr_Occurred())
646 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +0000647 Py_RETURN_TRUE;
648}
649
650PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
651
652static PyObject *
653set_issuperset(PySetObject *so, PyObject *other)
654{
Raymond Hettinger3fbec702003-11-21 07:56:36 +0000655 PyObject *tmp, *result;
656
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000657 if (!PyAnySet_Check(other)) {
Raymond Hettinger3fbec702003-11-21 07:56:36 +0000658 tmp = make_new_set(&PySet_Type, other);
659 if (tmp == NULL)
660 return NULL;
661 result = set_issuperset(so, tmp);
662 Py_DECREF(tmp);
663 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +0000664 }
665 return set_issubset((PySetObject *)other, (PyObject *)so);
666}
667
668PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
669
670static long
671set_nohash(PyObject *self)
672{
673 PyErr_SetString(PyExc_TypeError, "set objects are unhashable");
674 return -1;
675}
676
677static int
678set_nocmp(PyObject *self)
679{
680 PyErr_SetString(PyExc_TypeError, "cannot compare sets using cmp()");
681 return -1;
682}
683
684static long
685frozenset_hash(PyObject *self)
686{
687 PyObject *it, *item;
688 PySetObject *so = (PySetObject *)self;
Raymond Hettinger82d73dd2003-11-20 22:54:33 +0000689 long hash = 0, x;
Raymond Hettingera690a992003-11-16 16:17:49 +0000690
691 if (so->hash != -1)
692 return so->hash;
693
694 it = PyObject_GetIter(((PySetObject *)so)->data);
695 if (it == NULL)
696 return -1;
697
698 while ((item = PyIter_Next(it)) != NULL) {
Raymond Hettinger82d73dd2003-11-20 22:54:33 +0000699 x = PyObject_Hash(item);
700 /* Applying x*(x+1) breaks-up linear relationships so that
701 h(1) ^ h(2) will be less likely to coincide with hash(3).
702 Multiplying by a large prime increases the dispersion
703 between consecutive hashes. Adding one bit from the
704 original restores the one bit lost during the multiply
705 (all the products are even numbers). */
706 hash ^= (x * (x+1) * 3644798167) | (x&1);
Raymond Hettingera690a992003-11-16 16:17:49 +0000707 Py_DECREF(item);
708 }
Raymond Hettingera690a992003-11-16 16:17:49 +0000709 Py_DECREF(it);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000710 if (PyErr_Occurred())
711 return -1;
712 so->hash = hash;
Raymond Hettingera690a992003-11-16 16:17:49 +0000713 return hash;
714}
715
716static PyObject *
717set_richcompare(PySetObject *v, PyObject *w, int op)
718{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000719 if(!PyAnySet_Check(w)) {
720 if (op == Py_EQ)
721 Py_RETURN_FALSE;
722 if (op == Py_NE)
723 Py_RETURN_TRUE;
Raymond Hettingera690a992003-11-16 16:17:49 +0000724 PyErr_SetString(PyExc_TypeError, "can only compare to a set");
725 return NULL;
726 }
727 switch (op) {
728 case Py_EQ:
729 case Py_NE:
730 return PyObject_RichCompare(((PySetObject *)v)->data,
731 ((PySetObject *)w)->data, op);
732 case Py_LE:
733 return set_issubset((PySetObject *)v, w);
734 case Py_GE:
735 return set_issuperset((PySetObject *)v, w);
736 case Py_LT:
737 if (set_len(v) >= set_len((PySetObject *)w))
738 Py_RETURN_FALSE;
739 return set_issubset((PySetObject *)v, w);
740 case Py_GT:
741 if (set_len(v) <= set_len((PySetObject *)w))
742 Py_RETURN_FALSE;
743 return set_issuperset((PySetObject *)v, w);
744 }
745 Py_INCREF(Py_NotImplemented);
746 return Py_NotImplemented;
747}
748
749static PyObject *
750set_repr(PySetObject *so)
751{
752 PyObject *keys, *result, *listrepr;
753
754 keys = PyDict_Keys(so->data);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000755 if (keys == NULL)
756 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +0000757 listrepr = PyObject_Repr(keys);
758 Py_DECREF(keys);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000759 if (listrepr == NULL)
760 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +0000761
762 result = PyString_FromFormat("%s(%s)", so->ob_type->tp_name,
763 PyString_AS_STRING(listrepr));
764 Py_DECREF(listrepr);
765 return result;
766}
767
768static int
769set_tp_print(PySetObject *so, FILE *fp, int flags)
770{
771 PyObject *it, *item;
772 int firstpass=1;
773
774 it = PyObject_GetIter(so->data);
775 if (it == NULL)
776 return -1;
777 fprintf(fp, "%s([", so->ob_type->tp_name);
778
779 while ((item = PyIter_Next(it)) != NULL) {
780 if (firstpass == 1)
781 firstpass = 0;
782 else
Raymond Hettingere2c277a2003-11-16 16:36:58 +0000783 fprintf(fp, ", ");
Raymond Hettingera690a992003-11-16 16:17:49 +0000784 if (PyObject_Print(item, fp, 0) != 0) {
785 Py_DECREF(it);
786 Py_DECREF(item);
787 return -1;
788 }
789 Py_DECREF(item);
790 }
791 Py_DECREF(it);
792 fprintf(fp, "])");
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000793 if (PyErr_Occurred())
794 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000795 return 0;
796}
797
798static PyObject *
799set_clear(PySetObject *so)
800{
801 PyDict_Clear(so->data);
802 so->hash = -1;
803 Py_INCREF(Py_None);
804 return Py_None;
805}
806
807PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
808
809static int
810set_tp_clear(PySetObject *so)
811{
812 PyDict_Clear(so->data);
813 so->hash = -1;
814 return 0;
815}
816
817static PyObject *
818set_add(PySetObject *so, PyObject *item)
819{
820 if (PyDict_SetItem(so->data, item, Py_True) == -1)
821 return NULL;
822 Py_INCREF(Py_None);
823 return Py_None;
824}
825
826PyDoc_STRVAR(add_doc,
827"Add an element to a set.\n\
828\n\
829This has no effect if the element is already present.");
830
831static PyObject *
832set_remove(PySetObject *so, PyObject *item)
833{
Raymond Hettingerbfd334a2003-11-22 03:55:23 +0000834 PyObject *tmp;
835
836 if (PyDict_DelItem(so->data, item) == -1) {
837 if (!PyType_IsSubtype(item->ob_type, &PySet_Type))
838 return NULL;
839 PyErr_Clear();
840 tmp = frozenset_dict_wrapper(((PySetObject *)(item))->data);
841 if (tmp == NULL)
842 return NULL;
843 if (PyDict_DelItem(so->data, tmp) == -1) {
844 Py_DECREF(tmp);
845 return NULL;
846 }
847 Py_DECREF(tmp);
848 }
Raymond Hettingera690a992003-11-16 16:17:49 +0000849 Py_INCREF(Py_None);
850 return Py_None;
851}
852
853PyDoc_STRVAR(remove_doc,
854"Remove an element from a set; it must be a member.\n\
855\n\
856If the element is not a member, raise a KeyError.");
857
858static PyObject *
859set_discard(PySetObject *so, PyObject *item)
860{
Raymond Hettingerbfd334a2003-11-22 03:55:23 +0000861 PyObject *tmp;
862
Guido van Rossumb61982b2003-11-18 19:27:19 +0000863 if (PyDict_DelItem(so->data, item) == -1) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000864 if (PyErr_ExceptionMatches(PyExc_KeyError))
865 PyErr_Clear();
Raymond Hettingerbfd334a2003-11-22 03:55:23 +0000866 else {
867 if (!PyType_IsSubtype(item->ob_type, &PySet_Type))
868 return NULL;
869 PyErr_Clear();
870 tmp = frozenset_dict_wrapper(((PySetObject *)(item))->data);
871 if (tmp == NULL)
872 return NULL;
873 if (PyDict_DelItem(so->data, tmp) == -1) {
874 if (PyErr_ExceptionMatches(PyExc_KeyError))
875 PyErr_Clear();
876 else {
877 Py_DECREF(tmp);
878 return NULL;
879 }
880 }
881 Py_DECREF(tmp);
882 }
Guido van Rossumb61982b2003-11-18 19:27:19 +0000883 }
Raymond Hettingera690a992003-11-16 16:17:49 +0000884 Py_INCREF(Py_None);
885 return Py_None;
886}
887
888PyDoc_STRVAR(discard_doc,
889"Remove an element from a set if it is a member.\n\
890\n\
891If the element is not a member, do nothing.");
892
893static PyObject *
894set_pop(PySetObject *so)
895{
896 PyObject *key, *value;
897 int pos = 0;
898
899 if (!PyDict_Next(so->data, &pos, &key, &value)) {
900 PyErr_SetString(PyExc_KeyError, "pop from an empty set");
901 return NULL;
902 }
903 Py_INCREF(key);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000904 if (PyDict_DelItem(so->data, key) == -1) {
905 Py_DECREF(key);
906 return NULL;
907 }
Raymond Hettingera690a992003-11-16 16:17:49 +0000908 return key;
909}
910
911PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.");
912
913static PyObject *
914set_reduce(PySetObject *so)
915{
916 PyObject *keys=NULL, *args=NULL, *result=NULL;
917
918 keys = PyDict_Keys(so->data);
919 if (keys == NULL)
920 goto done;
921 args = PyTuple_Pack(1, keys);
922 if (args == NULL)
923 goto done;
924 result = PyTuple_Pack(2, so->ob_type, args);
925done:
926 Py_XDECREF(args);
927 Py_XDECREF(keys);
928 return result;
929}
930
931PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
932
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000933static int
934set_init(PySetObject *self, PyObject *args, PyObject *kwds)
935{
936 PyObject *iterable = NULL;
937 PyObject *result;
938
939 if (!PyAnySet_Check(self))
940 return -1;
941 if (!PyArg_UnpackTuple(args, self->ob_type->tp_name, 0, 1, &iterable))
942 return -1;
943 PyDict_Clear(self->data);
944 self->hash = -1;
945 if (iterable == NULL)
946 return 0;
947 result = set_union_update(self, iterable);
948 if (result != NULL) {
949 Py_DECREF(result);
950 return 0;
951 }
952 return -1;
953}
954
Raymond Hettingera690a992003-11-16 16:17:49 +0000955static PySequenceMethods set_as_sequence = {
956 (inquiry)set_len, /* sq_length */
957 0, /* sq_concat */
958 0, /* sq_repeat */
959 0, /* sq_item */
960 0, /* sq_slice */
961 0, /* sq_ass_item */
962 0, /* sq_ass_slice */
963 (objobjproc)set_contains, /* sq_contains */
964};
965
966/* set object ********************************************************/
967
968static PyMethodDef set_methods[] = {
969 {"add", (PyCFunction)set_add, METH_O,
970 add_doc},
971 {"clear", (PyCFunction)set_clear, METH_NOARGS,
972 clear_doc},
973 {"copy", (PyCFunction)set_copy, METH_NOARGS,
974 copy_doc},
975 {"__copy__", (PyCFunction)set_copy, METH_NOARGS,
976 copy_doc},
977 {"discard", (PyCFunction)set_discard, METH_O,
978 discard_doc},
979 {"difference", (PyCFunction)set_difference, METH_O,
980 difference_doc},
981 {"difference_update", (PyCFunction)set_difference_update, METH_O,
982 difference_update_doc},
983 {"intersection",(PyCFunction)set_intersection, METH_O,
984 intersection_doc},
985 {"intersection_update",(PyCFunction)set_intersection_update, METH_O,
986 intersection_update_doc},
987 {"issubset", (PyCFunction)set_issubset, METH_O,
988 issubset_doc},
989 {"issuperset", (PyCFunction)set_issuperset, METH_O,
990 issuperset_doc},
991 {"pop", (PyCFunction)set_pop, METH_NOARGS,
992 pop_doc},
993 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
994 reduce_doc},
995 {"remove", (PyCFunction)set_remove, METH_O,
996 remove_doc},
997 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
998 symmetric_difference_doc},
999 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
1000 symmetric_difference_update_doc},
1001 {"union", (PyCFunction)set_union, METH_O,
1002 union_doc},
1003 {"union_update",(PyCFunction)set_union_update, METH_O,
1004 union_update_doc},
1005 {NULL, NULL} /* sentinel */
1006};
1007
1008static PyNumberMethods set_as_number = {
1009 0, /*nb_add*/
1010 (binaryfunc)set_sub, /*nb_subtract*/
1011 0, /*nb_multiply*/
1012 0, /*nb_divide*/
1013 0, /*nb_remainder*/
1014 0, /*nb_divmod*/
1015 0, /*nb_power*/
1016 0, /*nb_negative*/
1017 0, /*nb_positive*/
1018 0, /*nb_absolute*/
1019 0, /*nb_nonzero*/
1020 0, /*nb_invert*/
1021 0, /*nb_lshift*/
1022 0, /*nb_rshift*/
1023 (binaryfunc)set_and, /*nb_and*/
1024 (binaryfunc)set_xor, /*nb_xor*/
1025 (binaryfunc)set_or, /*nb_or*/
1026 0, /*nb_coerce*/
1027 0, /*nb_int*/
1028 0, /*nb_long*/
1029 0, /*nb_float*/
1030 0, /*nb_oct*/
1031 0, /*nb_hex*/
1032 0, /*nb_inplace_add*/
1033 (binaryfunc)set_isub, /*nb_inplace_subtract*/
1034 0, /*nb_inplace_multiply*/
1035 0, /*nb_inplace_divide*/
1036 0, /*nb_inplace_remainder*/
1037 0, /*nb_inplace_power*/
1038 0, /*nb_inplace_lshift*/
1039 0, /*nb_inplace_rshift*/
1040 (binaryfunc)set_iand, /*nb_inplace_and*/
1041 (binaryfunc)set_ixor, /*nb_inplace_xor*/
1042 (binaryfunc)set_ior, /*nb_inplace_or*/
1043};
1044
1045PyDoc_STRVAR(set_doc,
1046"set(iterable) --> set object\n\
1047\n\
1048Build an unordered collection.");
1049
1050PyTypeObject PySet_Type = {
1051 PyObject_HEAD_INIT(&PyType_Type)
1052 0, /* ob_size */
1053 "set", /* 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 &set_as_number, /* tp_as_number */
1064 &set_as_sequence, /* tp_as_sequence */
1065 0, /* tp_as_mapping */
1066 set_nohash, /* 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 set_doc, /* tp_doc */
1075 (traverseproc)set_traverse, /* tp_traverse */
1076 (inquiry)set_tp_clear, /* tp_clear */
1077 (richcmpfunc)set_richcompare, /* tp_richcompare */
1078 0, /* tp_weaklistoffset */
1079 (getiterfunc)set_iter, /* tp_iter */
1080 0, /* tp_iternext */
1081 set_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 */
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001089 (initproc)set_init, /* tp_init */
Raymond Hettingera690a992003-11-16 16:17:49 +00001090 PyType_GenericAlloc, /* tp_alloc */
1091 set_new, /* tp_new */
1092 PyObject_GC_Del, /* tp_free */
1093};
1094
1095/* frozenset object ********************************************************/
1096
1097
1098static PyMethodDef frozenset_methods[] = {
1099 {"copy", (PyCFunction)set_copy, METH_NOARGS,
1100 copy_doc},
1101 {"__copy__", (PyCFunction)set_copy, METH_NOARGS,
1102 copy_doc},
1103 {"difference",(PyCFunction)set_difference, METH_O,
1104 difference_doc},
1105 {"intersection",(PyCFunction)set_intersection, METH_O,
1106 intersection_doc},
1107 {"issubset",(PyCFunction)set_issubset, METH_O,
1108 issubset_doc},
1109 {"issuperset",(PyCFunction)set_issuperset, METH_O,
1110 issuperset_doc},
1111 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
1112 reduce_doc},
1113 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
1114 symmetric_difference_doc},
1115 {"union", (PyCFunction)set_union, METH_O,
1116 union_doc},
1117 {NULL, NULL} /* sentinel */
1118};
1119
1120static PyNumberMethods frozenset_as_number = {
1121 0, /*nb_add*/
1122 (binaryfunc)set_sub, /*nb_subtract*/
1123 0, /*nb_multiply*/
1124 0, /*nb_divide*/
1125 0, /*nb_remainder*/
1126 0, /*nb_divmod*/
1127 0, /*nb_power*/
1128 0, /*nb_negative*/
1129 0, /*nb_positive*/
1130 0, /*nb_absolute*/
1131 0, /*nb_nonzero*/
1132 0, /*nb_invert*/
1133 0, /*nb_lshift*/
1134 0, /*nb_rshift*/
1135 (binaryfunc)set_and, /*nb_and*/
1136 (binaryfunc)set_xor, /*nb_xor*/
1137 (binaryfunc)set_or, /*nb_or*/
1138};
1139
1140PyDoc_STRVAR(frozenset_doc,
1141"frozenset(iterable) --> frozenset object\n\
1142\n\
1143Build an immutable unordered collection.");
1144
1145PyTypeObject PyFrozenSet_Type = {
1146 PyObject_HEAD_INIT(&PyType_Type)
1147 0, /* ob_size */
1148 "frozenset", /* tp_name */
1149 sizeof(PySetObject), /* tp_basicsize */
1150 0, /* tp_itemsize */
1151 /* methods */
1152 (destructor)set_dealloc, /* tp_dealloc */
1153 (printfunc)set_tp_print, /* tp_print */
1154 0, /* tp_getattr */
1155 0, /* tp_setattr */
1156 (cmpfunc)set_nocmp, /* tp_compare */
1157 (reprfunc)set_repr, /* tp_repr */
1158 &frozenset_as_number, /* tp_as_number */
1159 &set_as_sequence, /* tp_as_sequence */
1160 0, /* tp_as_mapping */
1161 frozenset_hash, /* tp_hash */
1162 0, /* tp_call */
1163 0, /* tp_str */
1164 PyObject_GenericGetAttr, /* tp_getattro */
1165 0, /* tp_setattro */
1166 0, /* tp_as_buffer */
1167 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES |
1168 Py_TPFLAGS_BASETYPE, /* tp_flags */
1169 frozenset_doc, /* tp_doc */
1170 (traverseproc)set_traverse, /* tp_traverse */
1171 0, /* tp_clear */
1172 (richcmpfunc)set_richcompare, /* tp_richcompare */
1173 0, /* tp_weaklistoffset */
1174 (getiterfunc)set_iter, /* tp_iter */
1175 0, /* tp_iternext */
1176 frozenset_methods, /* tp_methods */
1177 0, /* tp_members */
1178 0, /* tp_getset */
1179 0, /* tp_base */
1180 0, /* tp_dict */
1181 0, /* tp_descr_get */
1182 0, /* tp_descr_set */
1183 0, /* tp_dictoffset */
1184 0, /* tp_init */
1185 PyType_GenericAlloc, /* tp_alloc */
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001186 frozenset_new, /* tp_new */
Raymond Hettingera690a992003-11-16 16:17:49 +00001187 PyObject_GC_Del, /* tp_free */
1188};