blob: 01f05883e232bba7ff48eb7af9ea2590a68ae9de [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{
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +000015 PyObject *data = NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +000016 PyObject *it = NULL;
17 PyObject *item;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +000018 PySetObject *so = NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +000019
20 /* Get iterator. */
21 if (iterable != NULL) {
22 it = PyObject_GetIter(iterable);
23 if (it == NULL)
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +000024 goto done;
Raymond Hettingera690a992003-11-16 16:17:49 +000025 }
26
27 data = PyDict_New();
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +000028 if (data == NULL)
29 goto done;
Raymond Hettingera690a992003-11-16 16:17:49 +000030
31 while (it != NULL && (item = PyIter_Next(it)) != NULL) {
32 if (PyDict_SetItem(data, item, Py_True) == -1) {
Raymond Hettingera690a992003-11-16 16:17:49 +000033 Py_DECREF(item);
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +000034 goto done;
Raymond Hettingera690a992003-11-16 16:17:49 +000035 }
36 Py_DECREF(item);
37 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +000038 if (PyErr_Occurred())
39 goto done;
Raymond Hettingera690a992003-11-16 16:17:49 +000040
41 /* create PySetObject structure */
42 so = (PySetObject *)type->tp_alloc(type, 0);
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +000043 if (so == NULL)
44 goto done;
45
46 Py_INCREF(data);
Raymond Hettingera690a992003-11-16 16:17:49 +000047 so->data = data;
48 so->hash = -1;
49
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +000050done:
51 Py_XDECREF(data);
52 Py_XDECREF(it);
Raymond Hettingera690a992003-11-16 16:17:49 +000053 return (PyObject *)so;
54}
55
56static PyObject *
Raymond Hettinger50a4bb32003-11-17 16:42:33 +000057frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Raymond Hettingera690a992003-11-16 16:17:49 +000058{
59 PyObject *iterable = NULL;
60
61 if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
62 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +000063 if (iterable != NULL && PyFrozenSet_CheckExact(iterable)) {
Raymond Hettinger49ba4c32003-11-23 02:49:05 +000064 Py_INCREF(iterable);
65 return iterable;
66 }
Raymond Hettingera690a992003-11-16 16:17:49 +000067 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
Raymond Hettinger49ba4c32003-11-23 02:49:05 +0000157static PyObject *
158frozenset_copy(PySetObject *so)
159{
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000160 if (PyFrozenSet_CheckExact(so)) {
Raymond Hettinger49ba4c32003-11-23 02:49:05 +0000161 Py_INCREF(so);
162 return (PyObject *)so;
163 }
164 return set_copy(so);
165}
166
Raymond Hettingera690a992003-11-16 16:17:49 +0000167PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
168
169static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +0000170set_union_update(PySetObject *so, PyObject *other)
171{
172 PyObject *item, *data, *it;
173
Raymond Hettinger82d73dd2003-11-20 22:54:33 +0000174 if (PyAnySet_Check(other)) {
175 if (PyDict_Merge(so->data, ((PySetObject *)other)->data, 0) == -1)
176 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000177 Py_RETURN_NONE;
Raymond Hettinger82d73dd2003-11-20 22:54:33 +0000178 }
179
Raymond Hettingera690a992003-11-16 16:17:49 +0000180 it = PyObject_GetIter(other);
181 if (it == NULL)
182 return NULL;
183 data = so->data;
184
185 while ((item = PyIter_Next(it)) != NULL) {
186 if (PyDict_SetItem(data, item, Py_True) == -1) {
187 Py_DECREF(it);
188 Py_DECREF(item);
Raymond Hettingera690a992003-11-16 16:17:49 +0000189 return NULL;
190 }
191 Py_DECREF(item);
192 }
193 Py_DECREF(it);
194 if (PyErr_Occurred())
195 return NULL;
196 Py_RETURN_NONE;
197}
198
199PyDoc_STRVAR(union_update_doc,
200"Update a set with the union of itself and another.");
201
202static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000203set_union(PySetObject *so, PyObject *other)
204{
205 PySetObject *result;
206 PyObject *rv;
207
208 result = (PySetObject *)set_copy(so);
209 if (result == NULL)
210 return NULL;
211 rv = set_union_update(result, other);
212 if (rv == NULL) {
213 Py_DECREF(result);
214 return NULL;
215 }
216 Py_DECREF(rv);
217 return (PyObject *)result;
218}
219
220PyDoc_STRVAR(union_doc,
221 "Return the union of two sets as a new set.\n\
222\n\
223(i.e. all elements that are in either set.)");
224
225static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +0000226set_or(PySetObject *so, PyObject *other)
227{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000228 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000229 Py_INCREF(Py_NotImplemented);
230 return Py_NotImplemented;
231 }
232 return set_union(so, other);
233}
234
235static PyObject *
236set_ior(PySetObject *so, PyObject *other)
237{
238 PyObject *result;
239
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000240 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000241 Py_INCREF(Py_NotImplemented);
242 return Py_NotImplemented;
243 }
244 result = set_union_update(so, other);
245 if (result == NULL)
246 return NULL;
247 Py_DECREF(result);
248 Py_INCREF(so);
249 return (PyObject *)so;
250}
251
252static PyObject *
253set_intersection(PySetObject *so, PyObject *other)
254{
255 PySetObject *result;
256 PyObject *item, *selfdata, *tgtdata, *it;
257
258 result = (PySetObject *)make_new_set(so->ob_type, NULL);
259 if (result == NULL)
260 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000261 tgtdata = result->data;
262 selfdata = so->data;
263
264 if (PyAnySet_Check(other) &&
265 PyDict_Size(((PySetObject *)other)->data) > PyDict_Size(selfdata)) {
266 selfdata = ((PySetObject *)other)->data;
267 other = (PyObject *)so;
268 } else if (PyDict_Check(other) &&
269 PyDict_Size(other) > PyDict_Size(selfdata)) {
270 selfdata = other;
271 other = so->data;
272 }
273
Raymond Hettingera690a992003-11-16 16:17:49 +0000274 it = PyObject_GetIter(other);
275 if (it == NULL) {
276 Py_DECREF(result);
277 return NULL;
278 }
279
Raymond Hettingera690a992003-11-16 16:17:49 +0000280 while ((item = PyIter_Next(it)) != NULL) {
Raymond Hettinger1b92fd52003-11-18 14:15:31 +0000281 if (PySequence_Contains(selfdata, item)) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000282 if (PyDict_SetItem(tgtdata, item, Py_True) == -1) {
283 Py_DECREF(it);
284 Py_DECREF(result);
285 Py_DECREF(item);
Raymond Hettingera690a992003-11-16 16:17:49 +0000286 return NULL;
287 }
288 }
289 Py_DECREF(item);
290 }
291 Py_DECREF(it);
292 if (PyErr_Occurred()) {
293 Py_DECREF(result);
294 return NULL;
295 }
296 return (PyObject *)result;
297}
298
299PyDoc_STRVAR(intersection_doc,
300"Return the intersection of two sets as a new set.\n\
301\n\
302(i.e. all elements that are in both sets.)");
303
304static PyObject *
305set_intersection_update(PySetObject *so, PyObject *other)
306{
307 PyObject *item, *selfdata, *it, *newdict, *tmp;
308
309 newdict = PyDict_New();
310 if (newdict == NULL)
311 return newdict;
312
313 it = PyObject_GetIter(other);
314 if (it == NULL) {
315 Py_DECREF(newdict);
316 return NULL;
317 }
318
319 selfdata = so->data;
320 while ((item = PyIter_Next(it)) != NULL) {
Raymond Hettinger1b92fd52003-11-18 14:15:31 +0000321 if (PySequence_Contains(selfdata, item)) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000322 if (PyDict_SetItem(newdict, item, Py_True) == -1) {
323 Py_DECREF(newdict);
324 Py_DECREF(it);
325 Py_DECREF(item);
Raymond Hettingera690a992003-11-16 16:17:49 +0000326 return NULL;
327 }
328 }
329 Py_DECREF(item);
330 }
331 Py_DECREF(it);
332 if (PyErr_Occurred()) {
333 Py_DECREF(newdict);
334 return NULL;
335 }
336 tmp = so->data;
337 so->data = newdict;
338 Py_DECREF(tmp);
339 Py_RETURN_NONE;
340}
341
342PyDoc_STRVAR(intersection_update_doc,
343"Update a set with the intersection of itself and another.");
344
345static PyObject *
346set_and(PySetObject *so, PyObject *other)
347{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000348 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000349 Py_INCREF(Py_NotImplemented);
350 return Py_NotImplemented;
351 }
352 return set_intersection(so, other);
353}
354
355static PyObject *
356set_iand(PySetObject *so, PyObject *other)
357{
358 PyObject *result;
359
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000360 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000361 Py_INCREF(Py_NotImplemented);
362 return Py_NotImplemented;
363 }
364 result = set_intersection_update(so, other);
365 if (result == NULL)
366 return NULL;
367 Py_DECREF(result);
368 Py_INCREF(so);
369 return (PyObject *)so;
370}
371
372static PyObject *
373set_difference(PySetObject *so, PyObject *other)
374{
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000375 PySetObject *result, *otherset=NULL;
376 PyObject *item, *otherdata, *tgtdata, *it;
Raymond Hettingera690a992003-11-16 16:17:49 +0000377
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000378 result = (PySetObject *)make_new_set(so->ob_type, NULL);
Raymond Hettingera690a992003-11-16 16:17:49 +0000379 if (result == NULL)
380 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000381 tgtdata = result->data;
382
383 if (PyDict_Check(other))
384 otherdata = other;
385 else if (PyAnySet_Check(other))
386 otherdata = ((PySetObject *)other)->data;
387 else {
388 otherset = (PySetObject *)make_new_set(so->ob_type, other);
389 if (otherset == NULL) {
390 Py_DECREF(result);
391 return NULL;
392 }
393 otherdata = otherset->data;
394 }
395
396 it = PyObject_GetIter(so->data);
Raymond Hettingera690a992003-11-16 16:17:49 +0000397 if (it == NULL) {
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000398 Py_XDECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +0000399 Py_DECREF(result);
400 return NULL;
401 }
402
Raymond Hettingera690a992003-11-16 16:17:49 +0000403 while ((item = PyIter_Next(it)) != NULL) {
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000404 if (!PySequence_Contains(otherdata, item)) {
405 if (PyDict_SetItem(tgtdata, item, Py_True) == -1) {
406 Py_XDECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +0000407 Py_DECREF(it);
Raymond Hettingera690a992003-11-16 16:17:49 +0000408 Py_DECREF(item);
409 return NULL;
410 }
411 }
412 Py_DECREF(item);
413 }
414 Py_DECREF(it);
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000415 Py_XDECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +0000416 if (PyErr_Occurred()) {
417 Py_DECREF(result);
418 return NULL;
419 }
420 return (PyObject *)result;
421}
422
423PyDoc_STRVAR(difference_doc,
424"Return the difference of two sets as a new set.\n\
425\n\
426(i.e. all elements that are in this set but not the other.)");
427
428static PyObject *
429set_difference_update(PySetObject *so, PyObject *other)
430{
431 PyObject *item, *tgtdata, *it;
432
433 it = PyObject_GetIter(other);
434 if (it == NULL)
435 return NULL;
436
437 tgtdata = so->data;
438 while ((item = PyIter_Next(it)) != NULL) {
439 if (PyDict_DelItem(tgtdata, item) == -1) {
440 if (PyErr_ExceptionMatches(PyExc_KeyError))
441 PyErr_Clear();
442 else {
443 Py_DECREF(it);
444 Py_DECREF(item);
445 return NULL;
446 }
447 }
448 Py_DECREF(item);
449 }
450 Py_DECREF(it);
451 if (PyErr_Occurred())
452 return NULL;
453 Py_RETURN_NONE;
454}
455
456PyDoc_STRVAR(difference_update_doc,
457"Remove all elements of another set from this set.");
458
459static PyObject *
460set_sub(PySetObject *so, PyObject *other)
461{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000462 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000463 Py_INCREF(Py_NotImplemented);
464 return Py_NotImplemented;
465 }
466 return set_difference(so, other);
467}
468
469static PyObject *
470set_isub(PySetObject *so, PyObject *other)
471{
472 PyObject *result;
473
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000474 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000475 Py_INCREF(Py_NotImplemented);
476 return Py_NotImplemented;
477 }
478 result = set_difference_update(so, other);
479 if (result == NULL)
480 return NULL;
481 Py_DECREF(result);
482 Py_INCREF(so);
483 return (PyObject *)so;
484}
485
486static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +0000487set_symmetric_difference_update(PySetObject *so, PyObject *other)
488{
489 PyObject *item, *selfdata, *it, *otherdata;
490 PySetObject *otherset = NULL;
491
492 selfdata = so->data;
493
494 if (PyDict_Check(other))
495 otherdata = other;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000496 else if (PyAnySet_Check(other))
Raymond Hettingera690a992003-11-16 16:17:49 +0000497 otherdata = ((PySetObject *)other)->data;
498 else {
499 otherset = (PySetObject *)make_new_set(so->ob_type, other);
500 if (otherset == NULL)
501 return NULL;
502 otherdata = otherset->data;
503 }
504
505 it = PyObject_GetIter(otherdata);
506 if (it == NULL)
507 return NULL;
508
509 while ((item = PyIter_Next(it)) != NULL) {
Raymond Hettinger1b92fd52003-11-18 14:15:31 +0000510 if (PySequence_Contains(selfdata, item)) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000511 if (PyDict_DelItem(selfdata, item) == -1) {
512 Py_XDECREF(otherset);
513 Py_DECREF(it);
514 Py_DECREF(item);
Raymond Hettingera690a992003-11-16 16:17:49 +0000515 return NULL;
516 }
517 } else {
518 if (PyDict_SetItem(selfdata, item, Py_True) == -1) {
519 Py_XDECREF(otherset);
520 Py_DECREF(it);
521 Py_DECREF(item);
Raymond Hettingera690a992003-11-16 16:17:49 +0000522 return NULL;
523 }
524 }
525 Py_DECREF(item);
526 }
527 Py_XDECREF(otherset);
528 Py_DECREF(it);
529 if (PyErr_Occurred())
530 return NULL;
531 Py_RETURN_NONE;
532}
533
534PyDoc_STRVAR(symmetric_difference_update_doc,
535"Update a set with the symmetric difference of itself and another.");
536
537static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000538set_symmetric_difference(PySetObject *so, PyObject *other)
539{
540 PySetObject *result;
541 PyObject *item, *selfdata, *otherdata, *tgtdata, *it, *rv, *otherset;
542
543 if (PyDict_Check(other))
544 otherdata = other;
545 else if (PyAnySet_Check(other))
546 otherdata = ((PySetObject *)other)->data;
547 else {
548 otherset = make_new_set(so->ob_type, other);
549 if (otherset == NULL)
550 return NULL;
551 rv = set_symmetric_difference_update((PySetObject *)otherset, (PyObject *)so);
552 if (rv == NULL)
553 return NULL;
554 Py_DECREF(rv);
555 return otherset;
556 }
557
558 result = (PySetObject *)make_new_set(so->ob_type, NULL);
559 if (result == NULL)
560 return NULL;
561 tgtdata = result->data;
562 selfdata = so->data;
563
564 it = PyObject_GetIter(otherdata);
565 if (it == NULL) {
566 Py_DECREF(result);
567 return NULL;
568 }
569 while ((item = PyIter_Next(it)) != NULL) {
570 if (!PySequence_Contains(selfdata, item)) {
571 if (PyDict_SetItem(tgtdata, item, Py_True) == -1) {
572 Py_DECREF(it);
573 Py_DECREF(item);
574 return NULL;
575 }
576 }
577 Py_DECREF(item);
578 }
579 Py_DECREF(it);
580 if (PyErr_Occurred()) {
581 Py_DECREF(result);
582 return NULL;
583 }
584
585 it = PyObject_GetIter(selfdata);
586 if (it == NULL) {
587 Py_DECREF(result);
588 return NULL;
589 }
590 while ((item = PyIter_Next(it)) != NULL) {
591 if (!PySequence_Contains(otherdata, item)) {
592 if (PyDict_SetItem(tgtdata, item, Py_True) == -1) {
593 Py_DECREF(it);
594 Py_DECREF(item);
595 return NULL;
596 }
597 }
598 Py_DECREF(item);
599 }
600 Py_DECREF(it);
601 if (PyErr_Occurred()) {
602 Py_DECREF(result);
603 return NULL;
604 }
605
606 return (PyObject *)result;
607}
608
609PyDoc_STRVAR(symmetric_difference_doc,
610"Return the symmetric difference of two sets as a new set.\n\
611\n\
612(i.e. all elements that are in exactly one of the sets.)");
613
614static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +0000615set_xor(PySetObject *so, PyObject *other)
616{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000617 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000618 Py_INCREF(Py_NotImplemented);
619 return Py_NotImplemented;
620 }
621 return set_symmetric_difference(so, other);
622}
623
624static PyObject *
625set_ixor(PySetObject *so, PyObject *other)
626{
627 PyObject *result;
628
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000629 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000630 Py_INCREF(Py_NotImplemented);
631 return Py_NotImplemented;
632 }
633 result = set_symmetric_difference_update(so, other);
634 if (result == NULL)
635 return NULL;
636 Py_DECREF(result);
637 Py_INCREF(so);
638 return (PyObject *)so;
639}
640
641static PyObject *
642set_issubset(PySetObject *so, PyObject *other)
643{
Raymond Hettinger3fbec702003-11-21 07:56:36 +0000644 PyObject *otherdata, *it, *item, *tmp, *result;
Raymond Hettingera690a992003-11-16 16:17:49 +0000645
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_issubset(so, tmp);
651 Py_DECREF(tmp);
652 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +0000653 }
654 if (set_len(so) > set_len((PySetObject *)other))
655 Py_RETURN_FALSE;
656
657 it = PyObject_GetIter(so->data);
658 if (it == NULL)
659 return NULL;
660
661 otherdata = ((PySetObject *)other)->data;
662 while ((item = PyIter_Next(it)) != NULL) {
Raymond Hettinger1b92fd52003-11-18 14:15:31 +0000663 if (!PySequence_Contains(otherdata, item)) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000664 Py_DECREF(it);
665 Py_DECREF(item);
666 Py_RETURN_FALSE;
667 }
668 Py_DECREF(item);
669 }
670 Py_DECREF(it);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000671 if (PyErr_Occurred())
672 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +0000673 Py_RETURN_TRUE;
674}
675
676PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
677
678static PyObject *
679set_issuperset(PySetObject *so, PyObject *other)
680{
Raymond Hettinger3fbec702003-11-21 07:56:36 +0000681 PyObject *tmp, *result;
682
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000683 if (!PyAnySet_Check(other)) {
Raymond Hettinger3fbec702003-11-21 07:56:36 +0000684 tmp = make_new_set(&PySet_Type, other);
685 if (tmp == NULL)
686 return NULL;
687 result = set_issuperset(so, tmp);
688 Py_DECREF(tmp);
689 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +0000690 }
691 return set_issubset((PySetObject *)other, (PyObject *)so);
692}
693
694PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
695
696static long
697set_nohash(PyObject *self)
698{
699 PyErr_SetString(PyExc_TypeError, "set objects are unhashable");
700 return -1;
701}
702
703static int
704set_nocmp(PyObject *self)
705{
706 PyErr_SetString(PyExc_TypeError, "cannot compare sets using cmp()");
707 return -1;
708}
709
710static long
711frozenset_hash(PyObject *self)
712{
713 PyObject *it, *item;
714 PySetObject *so = (PySetObject *)self;
Raymond Hettinger49ba4c32003-11-23 02:49:05 +0000715 long hash = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +0000716
717 if (so->hash != -1)
718 return so->hash;
719
720 it = PyObject_GetIter(((PySetObject *)so)->data);
721 if (it == NULL)
722 return -1;
723
724 while ((item = PyIter_Next(it)) != NULL) {
Raymond Hettinger49ba4c32003-11-23 02:49:05 +0000725 /* Multiplying by a large prime increases the bit dispersion for
726 closely spaced hash values. The is important because some
727 use cases have many combinations of a small number of
728 elements with nearby hashes so that many distinct combinations
729 collapse to only a handful of distinct hash values. */
730 hash ^= PyObject_Hash(item) * 3644798167;
Raymond Hettingera690a992003-11-16 16:17:49 +0000731 Py_DECREF(item);
732 }
Raymond Hettingera690a992003-11-16 16:17:49 +0000733 Py_DECREF(it);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000734 if (PyErr_Occurred())
735 return -1;
736 so->hash = hash;
Raymond Hettingera690a992003-11-16 16:17:49 +0000737 return hash;
738}
739
740static PyObject *
741set_richcompare(PySetObject *v, PyObject *w, int op)
742{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000743 if(!PyAnySet_Check(w)) {
744 if (op == Py_EQ)
745 Py_RETURN_FALSE;
746 if (op == Py_NE)
747 Py_RETURN_TRUE;
Raymond Hettingera690a992003-11-16 16:17:49 +0000748 PyErr_SetString(PyExc_TypeError, "can only compare to a set");
749 return NULL;
750 }
751 switch (op) {
752 case Py_EQ:
753 case Py_NE:
754 return PyObject_RichCompare(((PySetObject *)v)->data,
755 ((PySetObject *)w)->data, op);
756 case Py_LE:
757 return set_issubset((PySetObject *)v, w);
758 case Py_GE:
759 return set_issuperset((PySetObject *)v, w);
760 case Py_LT:
761 if (set_len(v) >= set_len((PySetObject *)w))
762 Py_RETURN_FALSE;
763 return set_issubset((PySetObject *)v, w);
764 case Py_GT:
765 if (set_len(v) <= set_len((PySetObject *)w))
766 Py_RETURN_FALSE;
767 return set_issuperset((PySetObject *)v, w);
768 }
769 Py_INCREF(Py_NotImplemented);
770 return Py_NotImplemented;
771}
772
773static PyObject *
774set_repr(PySetObject *so)
775{
776 PyObject *keys, *result, *listrepr;
777
778 keys = PyDict_Keys(so->data);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000779 if (keys == NULL)
780 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +0000781 listrepr = PyObject_Repr(keys);
782 Py_DECREF(keys);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000783 if (listrepr == NULL)
784 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +0000785
786 result = PyString_FromFormat("%s(%s)", so->ob_type->tp_name,
787 PyString_AS_STRING(listrepr));
788 Py_DECREF(listrepr);
789 return result;
790}
791
792static int
793set_tp_print(PySetObject *so, FILE *fp, int flags)
794{
795 PyObject *it, *item;
796 int firstpass=1;
797
798 it = PyObject_GetIter(so->data);
799 if (it == NULL)
800 return -1;
801 fprintf(fp, "%s([", so->ob_type->tp_name);
802
803 while ((item = PyIter_Next(it)) != NULL) {
804 if (firstpass == 1)
805 firstpass = 0;
806 else
Raymond Hettingere2c277a2003-11-16 16:36:58 +0000807 fprintf(fp, ", ");
Raymond Hettingera690a992003-11-16 16:17:49 +0000808 if (PyObject_Print(item, fp, 0) != 0) {
809 Py_DECREF(it);
810 Py_DECREF(item);
811 return -1;
812 }
813 Py_DECREF(item);
814 }
815 Py_DECREF(it);
816 fprintf(fp, "])");
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000817 if (PyErr_Occurred())
818 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000819 return 0;
820}
821
822static PyObject *
823set_clear(PySetObject *so)
824{
825 PyDict_Clear(so->data);
826 so->hash = -1;
827 Py_INCREF(Py_None);
828 return Py_None;
829}
830
831PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
832
833static int
834set_tp_clear(PySetObject *so)
835{
836 PyDict_Clear(so->data);
837 so->hash = -1;
838 return 0;
839}
840
841static PyObject *
842set_add(PySetObject *so, PyObject *item)
843{
844 if (PyDict_SetItem(so->data, item, Py_True) == -1)
845 return NULL;
846 Py_INCREF(Py_None);
847 return Py_None;
848}
849
850PyDoc_STRVAR(add_doc,
851"Add an element to a set.\n\
852\n\
853This has no effect if the element is already present.");
854
855static PyObject *
856set_remove(PySetObject *so, PyObject *item)
857{
Raymond Hettingerbfd334a2003-11-22 03:55:23 +0000858 PyObject *tmp;
859
860 if (PyDict_DelItem(so->data, item) == -1) {
861 if (!PyType_IsSubtype(item->ob_type, &PySet_Type))
862 return NULL;
863 PyErr_Clear();
864 tmp = frozenset_dict_wrapper(((PySetObject *)(item))->data);
865 if (tmp == NULL)
866 return NULL;
867 if (PyDict_DelItem(so->data, tmp) == -1) {
868 Py_DECREF(tmp);
869 return NULL;
870 }
871 Py_DECREF(tmp);
872 }
Raymond Hettingera690a992003-11-16 16:17:49 +0000873 Py_INCREF(Py_None);
874 return Py_None;
875}
876
877PyDoc_STRVAR(remove_doc,
878"Remove an element from a set; it must be a member.\n\
879\n\
880If the element is not a member, raise a KeyError.");
881
882static PyObject *
883set_discard(PySetObject *so, PyObject *item)
884{
Raymond Hettingerbfd334a2003-11-22 03:55:23 +0000885 PyObject *tmp;
886
Guido van Rossumb61982b2003-11-18 19:27:19 +0000887 if (PyDict_DelItem(so->data, item) == -1) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000888 if (PyErr_ExceptionMatches(PyExc_KeyError))
889 PyErr_Clear();
Raymond Hettingerbfd334a2003-11-22 03:55:23 +0000890 else {
891 if (!PyType_IsSubtype(item->ob_type, &PySet_Type))
892 return NULL;
893 PyErr_Clear();
894 tmp = frozenset_dict_wrapper(((PySetObject *)(item))->data);
895 if (tmp == NULL)
896 return NULL;
897 if (PyDict_DelItem(so->data, tmp) == -1) {
898 if (PyErr_ExceptionMatches(PyExc_KeyError))
899 PyErr_Clear();
900 else {
901 Py_DECREF(tmp);
902 return NULL;
903 }
904 }
905 Py_DECREF(tmp);
906 }
Guido van Rossumb61982b2003-11-18 19:27:19 +0000907 }
Raymond Hettingera690a992003-11-16 16:17:49 +0000908 Py_INCREF(Py_None);
909 return Py_None;
910}
911
912PyDoc_STRVAR(discard_doc,
913"Remove an element from a set if it is a member.\n\
914\n\
915If the element is not a member, do nothing.");
916
917static PyObject *
918set_pop(PySetObject *so)
919{
920 PyObject *key, *value;
921 int pos = 0;
922
923 if (!PyDict_Next(so->data, &pos, &key, &value)) {
924 PyErr_SetString(PyExc_KeyError, "pop from an empty set");
925 return NULL;
926 }
927 Py_INCREF(key);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000928 if (PyDict_DelItem(so->data, key) == -1) {
929 Py_DECREF(key);
930 return NULL;
931 }
Raymond Hettingera690a992003-11-16 16:17:49 +0000932 return key;
933}
934
935PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.");
936
937static PyObject *
938set_reduce(PySetObject *so)
939{
940 PyObject *keys=NULL, *args=NULL, *result=NULL;
941
942 keys = PyDict_Keys(so->data);
943 if (keys == NULL)
944 goto done;
945 args = PyTuple_Pack(1, keys);
946 if (args == NULL)
947 goto done;
948 result = PyTuple_Pack(2, so->ob_type, args);
949done:
950 Py_XDECREF(args);
951 Py_XDECREF(keys);
952 return result;
953}
954
955PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
956
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000957static int
958set_init(PySetObject *self, PyObject *args, PyObject *kwds)
959{
960 PyObject *iterable = NULL;
961 PyObject *result;
962
963 if (!PyAnySet_Check(self))
964 return -1;
965 if (!PyArg_UnpackTuple(args, self->ob_type->tp_name, 0, 1, &iterable))
966 return -1;
967 PyDict_Clear(self->data);
968 self->hash = -1;
969 if (iterable == NULL)
970 return 0;
971 result = set_union_update(self, iterable);
972 if (result != NULL) {
973 Py_DECREF(result);
974 return 0;
975 }
976 return -1;
977}
978
Raymond Hettingera690a992003-11-16 16:17:49 +0000979static PySequenceMethods set_as_sequence = {
980 (inquiry)set_len, /* sq_length */
981 0, /* sq_concat */
982 0, /* sq_repeat */
983 0, /* sq_item */
984 0, /* sq_slice */
985 0, /* sq_ass_item */
986 0, /* sq_ass_slice */
987 (objobjproc)set_contains, /* sq_contains */
988};
989
990/* set object ********************************************************/
991
992static PyMethodDef set_methods[] = {
993 {"add", (PyCFunction)set_add, METH_O,
994 add_doc},
995 {"clear", (PyCFunction)set_clear, METH_NOARGS,
996 clear_doc},
997 {"copy", (PyCFunction)set_copy, METH_NOARGS,
998 copy_doc},
999 {"__copy__", (PyCFunction)set_copy, METH_NOARGS,
1000 copy_doc},
1001 {"discard", (PyCFunction)set_discard, METH_O,
1002 discard_doc},
1003 {"difference", (PyCFunction)set_difference, METH_O,
1004 difference_doc},
1005 {"difference_update", (PyCFunction)set_difference_update, METH_O,
1006 difference_update_doc},
1007 {"intersection",(PyCFunction)set_intersection, METH_O,
1008 intersection_doc},
1009 {"intersection_update",(PyCFunction)set_intersection_update, METH_O,
1010 intersection_update_doc},
1011 {"issubset", (PyCFunction)set_issubset, METH_O,
1012 issubset_doc},
1013 {"issuperset", (PyCFunction)set_issuperset, METH_O,
1014 issuperset_doc},
1015 {"pop", (PyCFunction)set_pop, METH_NOARGS,
1016 pop_doc},
1017 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
1018 reduce_doc},
1019 {"remove", (PyCFunction)set_remove, METH_O,
1020 remove_doc},
1021 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
1022 symmetric_difference_doc},
1023 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
1024 symmetric_difference_update_doc},
1025 {"union", (PyCFunction)set_union, METH_O,
1026 union_doc},
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001027 {"update", (PyCFunction)set_union_update, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00001028 union_update_doc},
1029 {NULL, NULL} /* sentinel */
1030};
1031
1032static PyNumberMethods set_as_number = {
1033 0, /*nb_add*/
1034 (binaryfunc)set_sub, /*nb_subtract*/
1035 0, /*nb_multiply*/
1036 0, /*nb_divide*/
1037 0, /*nb_remainder*/
1038 0, /*nb_divmod*/
1039 0, /*nb_power*/
1040 0, /*nb_negative*/
1041 0, /*nb_positive*/
1042 0, /*nb_absolute*/
1043 0, /*nb_nonzero*/
1044 0, /*nb_invert*/
1045 0, /*nb_lshift*/
1046 0, /*nb_rshift*/
1047 (binaryfunc)set_and, /*nb_and*/
1048 (binaryfunc)set_xor, /*nb_xor*/
1049 (binaryfunc)set_or, /*nb_or*/
1050 0, /*nb_coerce*/
1051 0, /*nb_int*/
1052 0, /*nb_long*/
1053 0, /*nb_float*/
1054 0, /*nb_oct*/
1055 0, /*nb_hex*/
1056 0, /*nb_inplace_add*/
1057 (binaryfunc)set_isub, /*nb_inplace_subtract*/
1058 0, /*nb_inplace_multiply*/
1059 0, /*nb_inplace_divide*/
1060 0, /*nb_inplace_remainder*/
1061 0, /*nb_inplace_power*/
1062 0, /*nb_inplace_lshift*/
1063 0, /*nb_inplace_rshift*/
1064 (binaryfunc)set_iand, /*nb_inplace_and*/
1065 (binaryfunc)set_ixor, /*nb_inplace_xor*/
1066 (binaryfunc)set_ior, /*nb_inplace_or*/
1067};
1068
1069PyDoc_STRVAR(set_doc,
1070"set(iterable) --> set object\n\
1071\n\
1072Build an unordered collection.");
1073
1074PyTypeObject PySet_Type = {
1075 PyObject_HEAD_INIT(&PyType_Type)
1076 0, /* ob_size */
1077 "set", /* tp_name */
1078 sizeof(PySetObject), /* tp_basicsize */
1079 0, /* tp_itemsize */
1080 /* methods */
1081 (destructor)set_dealloc, /* tp_dealloc */
1082 (printfunc)set_tp_print, /* tp_print */
1083 0, /* tp_getattr */
1084 0, /* tp_setattr */
1085 (cmpfunc)set_nocmp, /* tp_compare */
1086 (reprfunc)set_repr, /* tp_repr */
1087 &set_as_number, /* tp_as_number */
1088 &set_as_sequence, /* tp_as_sequence */
1089 0, /* tp_as_mapping */
1090 set_nohash, /* tp_hash */
1091 0, /* tp_call */
1092 0, /* tp_str */
1093 PyObject_GenericGetAttr, /* tp_getattro */
1094 0, /* tp_setattro */
1095 0, /* tp_as_buffer */
1096 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES |
1097 Py_TPFLAGS_BASETYPE, /* tp_flags */
1098 set_doc, /* tp_doc */
1099 (traverseproc)set_traverse, /* tp_traverse */
1100 (inquiry)set_tp_clear, /* tp_clear */
1101 (richcmpfunc)set_richcompare, /* tp_richcompare */
1102 0, /* tp_weaklistoffset */
1103 (getiterfunc)set_iter, /* tp_iter */
1104 0, /* tp_iternext */
1105 set_methods, /* tp_methods */
1106 0, /* tp_members */
1107 0, /* tp_getset */
1108 0, /* tp_base */
1109 0, /* tp_dict */
1110 0, /* tp_descr_get */
1111 0, /* tp_descr_set */
1112 0, /* tp_dictoffset */
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001113 (initproc)set_init, /* tp_init */
Raymond Hettingera690a992003-11-16 16:17:49 +00001114 PyType_GenericAlloc, /* tp_alloc */
1115 set_new, /* tp_new */
1116 PyObject_GC_Del, /* tp_free */
1117};
1118
1119/* frozenset object ********************************************************/
1120
1121
1122static PyMethodDef frozenset_methods[] = {
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001123 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
Raymond Hettingera690a992003-11-16 16:17:49 +00001124 copy_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001125 {"__copy__", (PyCFunction)frozenset_copy, METH_NOARGS,
Raymond Hettingera690a992003-11-16 16:17:49 +00001126 copy_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001127 {"difference", (PyCFunction)set_difference, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00001128 difference_doc},
1129 {"intersection",(PyCFunction)set_intersection, METH_O,
1130 intersection_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001131 {"issubset", (PyCFunction)set_issubset, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00001132 issubset_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001133 {"issuperset", (PyCFunction)set_issuperset, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00001134 issuperset_doc},
1135 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
1136 reduce_doc},
1137 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
1138 symmetric_difference_doc},
1139 {"union", (PyCFunction)set_union, METH_O,
1140 union_doc},
1141 {NULL, NULL} /* sentinel */
1142};
1143
1144static PyNumberMethods frozenset_as_number = {
1145 0, /*nb_add*/
1146 (binaryfunc)set_sub, /*nb_subtract*/
1147 0, /*nb_multiply*/
1148 0, /*nb_divide*/
1149 0, /*nb_remainder*/
1150 0, /*nb_divmod*/
1151 0, /*nb_power*/
1152 0, /*nb_negative*/
1153 0, /*nb_positive*/
1154 0, /*nb_absolute*/
1155 0, /*nb_nonzero*/
1156 0, /*nb_invert*/
1157 0, /*nb_lshift*/
1158 0, /*nb_rshift*/
1159 (binaryfunc)set_and, /*nb_and*/
1160 (binaryfunc)set_xor, /*nb_xor*/
1161 (binaryfunc)set_or, /*nb_or*/
1162};
1163
1164PyDoc_STRVAR(frozenset_doc,
1165"frozenset(iterable) --> frozenset object\n\
1166\n\
1167Build an immutable unordered collection.");
1168
1169PyTypeObject PyFrozenSet_Type = {
1170 PyObject_HEAD_INIT(&PyType_Type)
1171 0, /* ob_size */
1172 "frozenset", /* tp_name */
1173 sizeof(PySetObject), /* tp_basicsize */
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001174 0, /* tp_itemsize */ /* methods */
Raymond Hettingera690a992003-11-16 16:17:49 +00001175 (destructor)set_dealloc, /* tp_dealloc */
1176 (printfunc)set_tp_print, /* tp_print */
1177 0, /* tp_getattr */
1178 0, /* tp_setattr */
1179 (cmpfunc)set_nocmp, /* tp_compare */
1180 (reprfunc)set_repr, /* tp_repr */
1181 &frozenset_as_number, /* tp_as_number */
1182 &set_as_sequence, /* tp_as_sequence */
1183 0, /* tp_as_mapping */
1184 frozenset_hash, /* tp_hash */
1185 0, /* tp_call */
1186 0, /* tp_str */
1187 PyObject_GenericGetAttr, /* tp_getattro */
1188 0, /* tp_setattro */
1189 0, /* tp_as_buffer */
1190 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES |
1191 Py_TPFLAGS_BASETYPE, /* tp_flags */
1192 frozenset_doc, /* tp_doc */
1193 (traverseproc)set_traverse, /* tp_traverse */
1194 0, /* tp_clear */
1195 (richcmpfunc)set_richcompare, /* tp_richcompare */
1196 0, /* tp_weaklistoffset */
1197 (getiterfunc)set_iter, /* tp_iter */
1198 0, /* tp_iternext */
1199 frozenset_methods, /* tp_methods */
1200 0, /* tp_members */
1201 0, /* tp_getset */
1202 0, /* tp_base */
1203 0, /* tp_dict */
1204 0, /* tp_descr_get */
1205 0, /* tp_descr_set */
1206 0, /* tp_dictoffset */
1207 0, /* tp_init */
1208 PyType_GenericAlloc, /* tp_alloc */
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001209 frozenset_new, /* tp_new */
Raymond Hettingera690a992003-11-16 16:17:49 +00001210 PyObject_GC_Del, /* tp_free */
1211};