blob: f9448c960b33ab0aad84578ac092963701f46704 [file] [log] [blame]
Raymond Hettingera690a992003-11-16 16:17:49 +00001#include "Python.h"
Raymond Hettinger691d8052004-05-30 07:26:47 +00002#include "structmember.h"
Raymond Hettingera690a992003-11-16 16:17:49 +00003
4/* set object implementation
5 written and maintained by Raymond D. Hettinger <python@rcn.com>
6 derived from sets.py written by Greg V. Wilson, Alex Martelli,
7 Guido van Rossum, Raymond Hettinger, and Tim Peters.
8
9 Copyright (c) 2003 Python Software Foundation.
10 All rights reserved.
11*/
12
Raymond Hettingera690a992003-11-16 16:17:49 +000013static PyObject *
Raymond Hettingera38123e2003-11-24 22:18:49 +000014set_update(PySetObject *so, PyObject *other)
Raymond Hettingera690a992003-11-16 16:17:49 +000015{
Raymond Hettingera38123e2003-11-24 22:18:49 +000016 PyObject *item, *data, *it;
Raymond Hettingera690a992003-11-16 16:17:49 +000017
Raymond Hettingera38123e2003-11-24 22:18:49 +000018 if (PyAnySet_Check(other)) {
Raymond Hettinger81ad32e2003-12-15 21:16:06 +000019 if (PyDict_Merge(so->data, ((PySetObject *)other)->data, 1) == -1)
Raymond Hettingera38123e2003-11-24 22:18:49 +000020 return NULL;
21 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +000022 }
23
Raymond Hettingera38123e2003-11-24 22:18:49 +000024 it = PyObject_GetIter(other);
25 if (it == NULL)
26 return NULL;
27 data = so->data;
Raymond Hettingera690a992003-11-16 16:17:49 +000028
Raymond Hettingera38123e2003-11-24 22:18:49 +000029 while ((item = PyIter_Next(it)) != NULL) {
Raymond Hettingera690a992003-11-16 16:17:49 +000030 if (PyDict_SetItem(data, item, Py_True) == -1) {
Raymond Hettingera38123e2003-11-24 22:18:49 +000031 Py_DECREF(it);
Raymond Hettingera690a992003-11-16 16:17:49 +000032 Py_DECREF(item);
Raymond Hettingera38123e2003-11-24 22:18:49 +000033 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +000034 }
35 Py_DECREF(item);
36 }
Raymond Hettingera38123e2003-11-24 22:18:49 +000037 Py_DECREF(it);
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +000038 if (PyErr_Occurred())
Raymond Hettingera38123e2003-11-24 22:18:49 +000039 return NULL;
40 Py_RETURN_NONE;
41}
42
43PyDoc_STRVAR(update_doc,
44"Update a set with the union of itself and another.");
45
46static PyObject *
47make_new_set(PyTypeObject *type, PyObject *iterable)
48{
49 PyObject *data = NULL;
50 PyObject *tmp;
51 PySetObject *so = NULL;
52
53 data = PyDict_New();
54 if (data == NULL)
55 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +000056
57 /* create PySetObject structure */
58 so = (PySetObject *)type->tp_alloc(type, 0);
Raymond Hettingera38123e2003-11-24 22:18:49 +000059 if (so == NULL) {
60 Py_DECREF(data);
61 return NULL;
62 }
Raymond Hettingera690a992003-11-16 16:17:49 +000063 so->data = data;
64 so->hash = -1;
Raymond Hettinger691d8052004-05-30 07:26:47 +000065 so->weakreflist = NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +000066
Raymond Hettingera38123e2003-11-24 22:18:49 +000067 if (iterable != NULL) {
68 tmp = set_update(so, iterable);
69 if (tmp == NULL) {
70 Py_DECREF(so);
71 return NULL;
72 }
73 Py_DECREF(tmp);
74 }
75
Raymond Hettingera690a992003-11-16 16:17:49 +000076 return (PyObject *)so;
77}
78
79static PyObject *
Raymond Hettinger50a4bb32003-11-17 16:42:33 +000080frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Raymond Hettingera690a992003-11-16 16:17:49 +000081{
82 PyObject *iterable = NULL;
83
84 if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
85 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +000086 if (iterable != NULL && PyFrozenSet_CheckExact(iterable)) {
Raymond Hettinger49ba4c32003-11-23 02:49:05 +000087 Py_INCREF(iterable);
88 return iterable;
89 }
Raymond Hettingera690a992003-11-16 16:17:49 +000090 return make_new_set(type, iterable);
91}
92
Raymond Hettinger50a4bb32003-11-17 16:42:33 +000093static PyObject *
94set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
95{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +000096 return make_new_set(type, NULL);
97}
98
Raymond Hettingerbfd334a2003-11-22 03:55:23 +000099static PyObject *
100frozenset_dict_wrapper(PyObject *d)
101{
102 PySetObject *w;
103
104 assert(PyDict_Check(d));
105 w = (PySetObject *)make_new_set(&PyFrozenSet_Type, NULL);
106 if (w == NULL)
107 return NULL;
Raymond Hettinger6429a472004-09-28 01:51:35 +0000108 Py_CLEAR(w->data);
Raymond Hettingerbfd334a2003-11-22 03:55:23 +0000109 Py_INCREF(d);
110 w->data = d;
111 return (PyObject *)w;
112}
113
Raymond Hettingera690a992003-11-16 16:17:49 +0000114static void
115set_dealloc(PySetObject *so)
116{
Raymond Hettingerbb999b52005-06-18 21:00:26 +0000117 PyObject_GC_UnTrack(so);
Raymond Hettinger691d8052004-05-30 07:26:47 +0000118 if (so->weakreflist != NULL)
119 PyObject_ClearWeakRefs((PyObject *) so);
Raymond Hettingera690a992003-11-16 16:17:49 +0000120 Py_XDECREF(so->data);
121 so->ob_type->tp_free(so);
122}
123
Raymond Hettingerbb999b52005-06-18 21:00:26 +0000124static int
125set_traverse(PySetObject *so, visitproc visit, void *arg)
126{
127 if (so->data)
128 return visit(so->data, arg);
129 return 0;
130}
131
Raymond Hettingera690a992003-11-16 16:17:49 +0000132static PyObject *
133set_iter(PySetObject *so)
134{
135 return PyObject_GetIter(so->data);
136}
137
138static int
139set_len(PySetObject *so)
140{
141 return PyDict_Size(so->data);
142}
143
144static int
145set_contains(PySetObject *so, PyObject *key)
146{
Raymond Hettingerbfd334a2003-11-22 03:55:23 +0000147 PyObject *tmp;
Raymond Hettinger19c2d772003-11-21 18:36:54 +0000148 int result;
149
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +0000150 result = PyDict_Contains(so->data, key);
Raymond Hettingera38123e2003-11-24 22:18:49 +0000151 if (result == -1 && PyAnySet_Check(key)) {
Raymond Hettinger19c2d772003-11-21 18:36:54 +0000152 PyErr_Clear();
Raymond Hettingerbfd334a2003-11-22 03:55:23 +0000153 tmp = frozenset_dict_wrapper(((PySetObject *)(key))->data);
Raymond Hettinger19c2d772003-11-21 18:36:54 +0000154 if (tmp == NULL)
155 return -1;
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +0000156 result = PyDict_Contains(so->data, tmp);
Raymond Hettinger19c2d772003-11-21 18:36:54 +0000157 Py_DECREF(tmp);
158 }
159 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +0000160}
161
162static PyObject *
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +0000163set_direct_contains(PySetObject *so, PyObject *key)
164{
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +0000165 long result;
166
Raymond Hettinger438e02d2003-12-13 19:38:47 +0000167 result = set_contains(so, key);
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +0000168 if (result == -1)
169 return NULL;
170 return PyBool_FromLong(result);
171}
172
173PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
174
175static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +0000176set_copy(PySetObject *so)
177{
Raymond Hettingera38123e2003-11-24 22:18:49 +0000178 return make_new_set(so->ob_type, (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +0000179}
180
Raymond Hettinger49ba4c32003-11-23 02:49:05 +0000181static PyObject *
182frozenset_copy(PySetObject *so)
183{
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000184 if (PyFrozenSet_CheckExact(so)) {
Raymond Hettinger49ba4c32003-11-23 02:49:05 +0000185 Py_INCREF(so);
186 return (PyObject *)so;
187 }
188 return set_copy(so);
189}
190
Raymond Hettingera690a992003-11-16 16:17:49 +0000191PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
192
193static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000194set_union(PySetObject *so, PyObject *other)
195{
196 PySetObject *result;
197 PyObject *rv;
198
199 result = (PySetObject *)set_copy(so);
200 if (result == NULL)
201 return NULL;
Raymond Hettingera38123e2003-11-24 22:18:49 +0000202 rv = set_update(result, other);
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000203 if (rv == NULL) {
204 Py_DECREF(result);
205 return NULL;
206 }
207 Py_DECREF(rv);
208 return (PyObject *)result;
209}
210
211PyDoc_STRVAR(union_doc,
212 "Return the union of two sets as a new set.\n\
213\n\
214(i.e. all elements that are in either set.)");
215
216static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +0000217set_or(PySetObject *so, PyObject *other)
218{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000219 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000220 Py_INCREF(Py_NotImplemented);
221 return Py_NotImplemented;
222 }
223 return set_union(so, other);
224}
225
226static PyObject *
227set_ior(PySetObject *so, PyObject *other)
228{
229 PyObject *result;
230
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000231 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000232 Py_INCREF(Py_NotImplemented);
233 return Py_NotImplemented;
234 }
Raymond Hettingera38123e2003-11-24 22:18:49 +0000235 result = set_update(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +0000236 if (result == NULL)
237 return NULL;
238 Py_DECREF(result);
239 Py_INCREF(so);
240 return (PyObject *)so;
241}
242
243static PyObject *
244set_intersection(PySetObject *so, PyObject *other)
245{
246 PySetObject *result;
Raymond Hettingera3b11e72003-12-31 14:08:58 +0000247 PyObject *item, *selfdata, *tgtdata, *it, *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +0000248
249 result = (PySetObject *)make_new_set(so->ob_type, NULL);
250 if (result == NULL)
251 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000252 tgtdata = result->data;
253 selfdata = so->data;
254
Raymond Hettingera3b11e72003-12-31 14:08:58 +0000255 if (PyAnySet_Check(other))
256 other = ((PySetObject *)other)->data;
257
258 if (PyDict_Check(other) && PyDict_Size(other) > PyDict_Size(selfdata)) {
259 tmp = selfdata;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000260 selfdata = other;
Raymond Hettingera3b11e72003-12-31 14:08:58 +0000261 other = tmp;
262 }
263
264 if (PyDict_CheckExact(other)) {
265 PyObject *value;
266 int pos = 0;
267 while (PyDict_Next(other, &pos, &item, &value)) {
268 if (PyDict_Contains(selfdata, item)) {
269 if (PyDict_SetItem(tgtdata, item, Py_True) == -1) {
270 Py_DECREF(result);
271 return NULL;
272 }
273 }
274 }
275 return (PyObject *)result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000276 }
277
Raymond Hettingera690a992003-11-16 16:17:49 +0000278 it = PyObject_GetIter(other);
279 if (it == NULL) {
280 Py_DECREF(result);
281 return NULL;
282 }
283
Raymond Hettingera690a992003-11-16 16:17:49 +0000284 while ((item = PyIter_Next(it)) != NULL) {
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +0000285 if (PyDict_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 Hettingerbc0f2ab2003-11-25 21:12:14 +0000325 if (PyDict_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 *
Raymond Hettingera690a992003-11-16 16:17:49 +0000377set_difference_update(PySetObject *so, PyObject *other)
378{
379 PyObject *item, *tgtdata, *it;
380
381 it = PyObject_GetIter(other);
382 if (it == NULL)
383 return NULL;
384
385 tgtdata = so->data;
386 while ((item = PyIter_Next(it)) != NULL) {
387 if (PyDict_DelItem(tgtdata, item) == -1) {
388 if (PyErr_ExceptionMatches(PyExc_KeyError))
389 PyErr_Clear();
390 else {
391 Py_DECREF(it);
392 Py_DECREF(item);
393 return NULL;
394 }
395 }
396 Py_DECREF(item);
397 }
398 Py_DECREF(it);
399 if (PyErr_Occurred())
400 return NULL;
401 Py_RETURN_NONE;
402}
403
404PyDoc_STRVAR(difference_update_doc,
405"Remove all elements of another set from this set.");
406
407static PyObject *
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +0000408set_difference(PySetObject *so, PyObject *other)
409{
410 PyObject *result, *tmp;
411 PyObject *otherdata, *tgtdata;
412 PyObject *key, *value;
413 int pos = 0;
414
415 if (PyDict_Check(other))
416 otherdata = other;
417 else if (PyAnySet_Check(other))
418 otherdata = ((PySetObject *)other)->data;
419 else {
420 result = set_copy(so);
421 if (result == NULL)
422 return result;
423 tmp = set_difference_update((PySetObject *)result, other);
424 if (tmp != NULL) {
425 Py_DECREF(tmp);
426 return result;
427 }
428 Py_DECREF(result);
429 return NULL;
430 }
431
432 result = make_new_set(so->ob_type, NULL);
433 if (result == NULL)
434 return NULL;
435 tgtdata = ((PySetObject *)result)->data;
436
437 while (PyDict_Next(so->data, &pos, &key, &value)) {
438 if (!PyDict_Contains(otherdata, key)) {
439 if (PyDict_SetItem(tgtdata, key, Py_True) == -1)
440 return NULL;
441 }
442 }
443 return result;
444}
445
446PyDoc_STRVAR(difference_doc,
447"Return the difference of two sets as a new set.\n\
448\n\
449(i.e. all elements that are in this set but not the other.)");
450static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +0000451set_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 *
Raymond Hettingera690a992003-11-16 16:17:49 +0000478set_symmetric_difference_update(PySetObject *so, PyObject *other)
479{
Raymond Hettingerdc5ae112003-12-13 14:46:46 +0000480 PyObject *selfdata, *otherdata;
Raymond Hettingera690a992003-11-16 16:17:49 +0000481 PySetObject *otherset = NULL;
Raymond Hettingerdc5ae112003-12-13 14:46:46 +0000482 PyObject *key, *value;
483 int pos = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +0000484
485 selfdata = so->data;
Raymond Hettingera690a992003-11-16 16:17:49 +0000486 if (PyDict_Check(other))
487 otherdata = other;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000488 else if (PyAnySet_Check(other))
Raymond Hettingera690a992003-11-16 16:17:49 +0000489 otherdata = ((PySetObject *)other)->data;
490 else {
491 otherset = (PySetObject *)make_new_set(so->ob_type, other);
492 if (otherset == NULL)
493 return NULL;
494 otherdata = otherset->data;
495 }
496
Raymond Hettingerdc5ae112003-12-13 14:46:46 +0000497 while (PyDict_Next(otherdata, &pos, &key, &value)) {
498 if (PyDict_Contains(selfdata, key)) {
499 if (PyDict_DelItem(selfdata, key) == -1) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000500 Py_XDECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +0000501 return NULL;
502 }
503 } else {
Raymond Hettingerdc5ae112003-12-13 14:46:46 +0000504 if (PyDict_SetItem(selfdata, key, Py_True) == -1) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000505 Py_XDECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +0000506 return NULL;
507 }
508 }
Raymond Hettingera690a992003-11-16 16:17:49 +0000509 }
510 Py_XDECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +0000511 Py_RETURN_NONE;
512}
513
514PyDoc_STRVAR(symmetric_difference_update_doc,
515"Update a set with the symmetric difference of itself and another.");
516
517static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000518set_symmetric_difference(PySetObject *so, PyObject *other)
519{
520 PySetObject *result;
Raymond Hettingerdc5ae112003-12-13 14:46:46 +0000521 PyObject *selfdata, *otherdata, *tgtdata, *rv, *otherset;
522 PyObject *key, *value;
523 int pos = 0;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000524
525 if (PyDict_Check(other))
526 otherdata = other;
527 else if (PyAnySet_Check(other))
528 otherdata = ((PySetObject *)other)->data;
529 else {
530 otherset = make_new_set(so->ob_type, other);
531 if (otherset == NULL)
532 return NULL;
533 rv = set_symmetric_difference_update((PySetObject *)otherset, (PyObject *)so);
534 if (rv == NULL)
535 return NULL;
536 Py_DECREF(rv);
537 return otherset;
538 }
539
540 result = (PySetObject *)make_new_set(so->ob_type, NULL);
541 if (result == NULL)
542 return NULL;
543 tgtdata = result->data;
544 selfdata = so->data;
545
Raymond Hettingerdc5ae112003-12-13 14:46:46 +0000546 while (PyDict_Next(otherdata, &pos, &key, &value)) {
547 if (!PyDict_Contains(selfdata, key)) {
548 if (PyDict_SetItem(tgtdata, key, Py_True) == -1) {
549 Py_DECREF(result);
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000550 return NULL;
551 }
552 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000553 }
554
Raymond Hettingerdc5ae112003-12-13 14:46:46 +0000555 pos = 0;
556 while (PyDict_Next(selfdata, &pos, &key, &value)) {
557 if (!PyDict_Contains(otherdata, key)) {
558 if (PyDict_SetItem(tgtdata, key, Py_True) == -1) {
559 Py_DECREF(result);
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000560 return NULL;
561 }
562 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000563 }
564
565 return (PyObject *)result;
566}
567
568PyDoc_STRVAR(symmetric_difference_doc,
569"Return the symmetric difference of two sets as a new set.\n\
570\n\
571(i.e. all elements that are in exactly one of the sets.)");
572
573static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +0000574set_xor(PySetObject *so, PyObject *other)
575{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000576 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000577 Py_INCREF(Py_NotImplemented);
578 return Py_NotImplemented;
579 }
580 return set_symmetric_difference(so, other);
581}
582
583static PyObject *
584set_ixor(PySetObject *so, PyObject *other)
585{
586 PyObject *result;
587
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000588 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000589 Py_INCREF(Py_NotImplemented);
590 return Py_NotImplemented;
591 }
592 result = set_symmetric_difference_update(so, other);
593 if (result == NULL)
594 return NULL;
595 Py_DECREF(result);
596 Py_INCREF(so);
597 return (PyObject *)so;
598}
599
600static PyObject *
601set_issubset(PySetObject *so, PyObject *other)
602{
Raymond Hettingerdc5ae112003-12-13 14:46:46 +0000603 PyObject *otherdata, *tmp, *result;
604 PyObject *key, *value;
605 int pos = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +0000606
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000607 if (!PyAnySet_Check(other)) {
Raymond Hettinger3fbec702003-11-21 07:56:36 +0000608 tmp = make_new_set(&PySet_Type, other);
609 if (tmp == NULL)
610 return NULL;
611 result = set_issubset(so, tmp);
612 Py_DECREF(tmp);
613 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +0000614 }
615 if (set_len(so) > set_len((PySetObject *)other))
616 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +0000617
618 otherdata = ((PySetObject *)other)->data;
Raymond Hettingerdc5ae112003-12-13 14:46:46 +0000619 while (PyDict_Next(((PySetObject *)so)->data, &pos, &key, &value)) {
620 if (!PyDict_Contains(otherdata, key))
Raymond Hettingera690a992003-11-16 16:17:49 +0000621 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +0000622 }
Raymond Hettingera690a992003-11-16 16:17:49 +0000623 Py_RETURN_TRUE;
624}
625
626PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
627
628static PyObject *
629set_issuperset(PySetObject *so, PyObject *other)
630{
Raymond Hettinger3fbec702003-11-21 07:56:36 +0000631 PyObject *tmp, *result;
632
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000633 if (!PyAnySet_Check(other)) {
Raymond Hettinger3fbec702003-11-21 07:56:36 +0000634 tmp = make_new_set(&PySet_Type, other);
635 if (tmp == NULL)
636 return NULL;
637 result = set_issuperset(so, tmp);
638 Py_DECREF(tmp);
639 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +0000640 }
641 return set_issubset((PySetObject *)other, (PyObject *)so);
642}
643
644PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
645
646static long
647set_nohash(PyObject *self)
648{
649 PyErr_SetString(PyExc_TypeError, "set objects are unhashable");
650 return -1;
651}
652
653static int
654set_nocmp(PyObject *self)
655{
656 PyErr_SetString(PyExc_TypeError, "cannot compare sets using cmp()");
657 return -1;
658}
659
660static long
661frozenset_hash(PyObject *self)
662{
Raymond Hettingera690a992003-11-16 16:17:49 +0000663 PySetObject *so = (PySetObject *)self;
Raymond Hettingerdc5ae112003-12-13 14:46:46 +0000664 PyObject *key, *value;
665 int pos = 0;
Raymond Hettingerc9786332004-06-10 22:41:48 +0000666 long hash = 1927868237L;
Raymond Hettinger438e02d2003-12-13 19:38:47 +0000667
Raymond Hettingera690a992003-11-16 16:17:49 +0000668 if (so->hash != -1)
669 return so->hash;
Raymond Hettingera690a992003-11-16 16:17:49 +0000670
Raymond Hettingerc9786332004-06-10 22:41:48 +0000671 hash *= (PyDict_Size(so->data) + 1);
Raymond Hettingerdc5ae112003-12-13 14:46:46 +0000672 while (PyDict_Next(so->data, &pos, &key, &value)) {
Raymond Hettingerc9786332004-06-10 22:41:48 +0000673 /* Work to increase the bit dispersion for closely spaced hash
674 values. The is important because some use cases have many
675 combinations of a small number of elements with nearby
676 hashes so that many distinct combinations collapse to only
677 a handful of distinct hash values. */
678 long h = PyObject_Hash(key);
679 hash ^= (h ^ (h << 16) ^ 89869747L) * 3644798167u;
Raymond Hettingera690a992003-11-16 16:17:49 +0000680 }
Raymond Hettingerc9786332004-06-10 22:41:48 +0000681 hash = hash * 69069L + 907133923L;
Raymond Hettinger27e403e2004-06-10 21:38:41 +0000682 if (hash == -1)
683 hash = 590923713L;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000684 so->hash = hash;
Raymond Hettingera690a992003-11-16 16:17:49 +0000685 return hash;
686}
687
688static PyObject *
689set_richcompare(PySetObject *v, PyObject *w, int op)
690{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000691 if(!PyAnySet_Check(w)) {
692 if (op == Py_EQ)
693 Py_RETURN_FALSE;
694 if (op == Py_NE)
695 Py_RETURN_TRUE;
Raymond Hettingera690a992003-11-16 16:17:49 +0000696 PyErr_SetString(PyExc_TypeError, "can only compare to a set");
697 return NULL;
698 }
699 switch (op) {
700 case Py_EQ:
701 case Py_NE:
702 return PyObject_RichCompare(((PySetObject *)v)->data,
703 ((PySetObject *)w)->data, op);
704 case Py_LE:
705 return set_issubset((PySetObject *)v, w);
706 case Py_GE:
707 return set_issuperset((PySetObject *)v, w);
708 case Py_LT:
709 if (set_len(v) >= set_len((PySetObject *)w))
710 Py_RETURN_FALSE;
711 return set_issubset((PySetObject *)v, w);
712 case Py_GT:
713 if (set_len(v) <= set_len((PySetObject *)w))
714 Py_RETURN_FALSE;
715 return set_issuperset((PySetObject *)v, w);
716 }
717 Py_INCREF(Py_NotImplemented);
718 return Py_NotImplemented;
719}
720
721static PyObject *
722set_repr(PySetObject *so)
723{
724 PyObject *keys, *result, *listrepr;
725
726 keys = PyDict_Keys(so->data);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000727 if (keys == NULL)
728 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +0000729 listrepr = PyObject_Repr(keys);
730 Py_DECREF(keys);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000731 if (listrepr == NULL)
732 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +0000733
734 result = PyString_FromFormat("%s(%s)", so->ob_type->tp_name,
735 PyString_AS_STRING(listrepr));
736 Py_DECREF(listrepr);
737 return result;
738}
739
740static int
741set_tp_print(PySetObject *so, FILE *fp, int flags)
742{
Raymond Hettingerdc5ae112003-12-13 14:46:46 +0000743 PyObject *key, *value;
Raymond Hettingera3b11e72003-12-31 14:08:58 +0000744 int pos=0;
745 char *emit = ""; /* No separator emitted on first pass */
746 char *separator = ", ";
Raymond Hettingera690a992003-11-16 16:17:49 +0000747
Raymond Hettingera690a992003-11-16 16:17:49 +0000748 fprintf(fp, "%s([", so->ob_type->tp_name);
Raymond Hettingerdc5ae112003-12-13 14:46:46 +0000749 while (PyDict_Next(so->data, &pos, &key, &value)) {
Raymond Hettingera3b11e72003-12-31 14:08:58 +0000750 fputs(emit, fp);
751 emit = separator;
Raymond Hettingerdc5ae112003-12-13 14:46:46 +0000752 if (PyObject_Print(key, fp, 0) != 0)
Raymond Hettingera690a992003-11-16 16:17:49 +0000753 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000754 }
Raymond Hettingera3b11e72003-12-31 14:08:58 +0000755 fputs("])", fp);
Raymond Hettingera690a992003-11-16 16:17:49 +0000756 return 0;
757}
758
759static PyObject *
760set_clear(PySetObject *so)
761{
762 PyDict_Clear(so->data);
763 so->hash = -1;
Raymond Hettinger438e02d2003-12-13 19:38:47 +0000764 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +0000765}
766
767PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
768
Raymond Hettingerbb999b52005-06-18 21:00:26 +0000769static int
770set_tp_clear(PySetObject *so)
771{
772 PyDict_Clear(so->data);
773 so->hash = -1;
774 return 0;
775}
776
Raymond Hettingera690a992003-11-16 16:17:49 +0000777static PyObject *
778set_add(PySetObject *so, PyObject *item)
779{
780 if (PyDict_SetItem(so->data, item, Py_True) == -1)
781 return NULL;
Raymond Hettinger438e02d2003-12-13 19:38:47 +0000782 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +0000783}
784
785PyDoc_STRVAR(add_doc,
786"Add an element to a set.\n\
787\n\
788This has no effect if the element is already present.");
789
790static PyObject *
791set_remove(PySetObject *so, PyObject *item)
792{
Raymond Hettinger0deab622003-12-13 18:53:18 +0000793 PyObject *tmp, *result;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +0000794
Raymond Hettinger0deab622003-12-13 18:53:18 +0000795 if (PyType_IsSubtype(item->ob_type, &PySet_Type)) {
Raymond Hettingerbfd334a2003-11-22 03:55:23 +0000796 tmp = frozenset_dict_wrapper(((PySetObject *)(item))->data);
797 if (tmp == NULL)
798 return NULL;
Raymond Hettinger0deab622003-12-13 18:53:18 +0000799 result = set_remove(so, tmp);
Raymond Hettingerbfd334a2003-11-22 03:55:23 +0000800 Py_DECREF(tmp);
Raymond Hettinger0deab622003-12-13 18:53:18 +0000801 return result;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +0000802 }
Raymond Hettinger0deab622003-12-13 18:53:18 +0000803
804 if (PyDict_DelItem(so->data, item) == -1)
805 return NULL;
Raymond Hettinger438e02d2003-12-13 19:38:47 +0000806 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +0000807}
808
809PyDoc_STRVAR(remove_doc,
810"Remove an element from a set; it must be a member.\n\
811\n\
812If the element is not a member, raise a KeyError.");
813
814static PyObject *
815set_discard(PySetObject *so, PyObject *item)
816{
Raymond Hettinger0deab622003-12-13 18:53:18 +0000817 PyObject *tmp, *result;
818
819 if (PyType_IsSubtype(item->ob_type, &PySet_Type)) {
820 tmp = frozenset_dict_wrapper(((PySetObject *)(item))->data);
821 if (tmp == NULL)
822 return NULL;
823 result = set_discard(so, tmp);
824 Py_DECREF(tmp);
825 return result;
826 }
Raymond Hettingerbfd334a2003-11-22 03:55:23 +0000827
Guido van Rossumb61982b2003-11-18 19:27:19 +0000828 if (PyDict_DelItem(so->data, item) == -1) {
Raymond Hettinger0deab622003-12-13 18:53:18 +0000829 if (!PyErr_ExceptionMatches(PyExc_KeyError))
830 return NULL;
831 PyErr_Clear();
Guido van Rossumb61982b2003-11-18 19:27:19 +0000832 }
Raymond Hettinger438e02d2003-12-13 19:38:47 +0000833 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +0000834}
835
836PyDoc_STRVAR(discard_doc,
837"Remove an element from a set if it is a member.\n\
838\n\
839If the element is not a member, do nothing.");
840
841static PyObject *
842set_pop(PySetObject *so)
843{
844 PyObject *key, *value;
845 int pos = 0;
846
847 if (!PyDict_Next(so->data, &pos, &key, &value)) {
848 PyErr_SetString(PyExc_KeyError, "pop from an empty set");
849 return NULL;
850 }
851 Py_INCREF(key);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000852 if (PyDict_DelItem(so->data, key) == -1) {
853 Py_DECREF(key);
854 return NULL;
855 }
Raymond Hettingera690a992003-11-16 16:17:49 +0000856 return key;
857}
858
859PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.");
860
861static PyObject *
862set_reduce(PySetObject *so)
863{
Raymond Hettinger15056a52004-11-09 07:25:31 +0000864 PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +0000865
866 keys = PyDict_Keys(so->data);
867 if (keys == NULL)
868 goto done;
869 args = PyTuple_Pack(1, keys);
870 if (args == NULL)
871 goto done;
Raymond Hettinger15056a52004-11-09 07:25:31 +0000872 dict = PyObject_GetAttrString((PyObject *)so, "__dict__");
873 if (dict == NULL) {
874 PyErr_Clear();
875 dict = Py_None;
876 Py_INCREF(dict);
877 }
878 result = PyTuple_Pack(3, so->ob_type, args, dict);
Raymond Hettingera690a992003-11-16 16:17:49 +0000879done:
880 Py_XDECREF(args);
881 Py_XDECREF(keys);
Raymond Hettinger15056a52004-11-09 07:25:31 +0000882 Py_XDECREF(dict);
Raymond Hettingera690a992003-11-16 16:17:49 +0000883 return result;
884}
885
886PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
887
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000888static int
889set_init(PySetObject *self, PyObject *args, PyObject *kwds)
890{
891 PyObject *iterable = NULL;
892 PyObject *result;
893
894 if (!PyAnySet_Check(self))
895 return -1;
896 if (!PyArg_UnpackTuple(args, self->ob_type->tp_name, 0, 1, &iterable))
897 return -1;
898 PyDict_Clear(self->data);
899 self->hash = -1;
900 if (iterable == NULL)
901 return 0;
Raymond Hettingera38123e2003-11-24 22:18:49 +0000902 result = set_update(self, iterable);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000903 if (result != NULL) {
904 Py_DECREF(result);
905 return 0;
906 }
907 return -1;
908}
909
Raymond Hettingera690a992003-11-16 16:17:49 +0000910static PySequenceMethods set_as_sequence = {
911 (inquiry)set_len, /* sq_length */
912 0, /* sq_concat */
913 0, /* sq_repeat */
914 0, /* sq_item */
915 0, /* sq_slice */
916 0, /* sq_ass_item */
917 0, /* sq_ass_slice */
918 (objobjproc)set_contains, /* sq_contains */
919};
920
921/* set object ********************************************************/
922
923static PyMethodDef set_methods[] = {
924 {"add", (PyCFunction)set_add, METH_O,
925 add_doc},
926 {"clear", (PyCFunction)set_clear, METH_NOARGS,
927 clear_doc},
Raymond Hettinger0deab622003-12-13 18:53:18 +0000928 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +0000929 contains_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +0000930 {"copy", (PyCFunction)set_copy, METH_NOARGS,
931 copy_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +0000932 {"discard", (PyCFunction)set_discard, METH_O,
933 discard_doc},
934 {"difference", (PyCFunction)set_difference, METH_O,
935 difference_doc},
936 {"difference_update", (PyCFunction)set_difference_update, METH_O,
937 difference_update_doc},
938 {"intersection",(PyCFunction)set_intersection, METH_O,
939 intersection_doc},
940 {"intersection_update",(PyCFunction)set_intersection_update, METH_O,
941 intersection_update_doc},
942 {"issubset", (PyCFunction)set_issubset, METH_O,
943 issubset_doc},
944 {"issuperset", (PyCFunction)set_issuperset, METH_O,
945 issuperset_doc},
946 {"pop", (PyCFunction)set_pop, METH_NOARGS,
947 pop_doc},
948 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
949 reduce_doc},
950 {"remove", (PyCFunction)set_remove, METH_O,
951 remove_doc},
952 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
953 symmetric_difference_doc},
954 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
955 symmetric_difference_update_doc},
956 {"union", (PyCFunction)set_union, METH_O,
957 union_doc},
Raymond Hettingera38123e2003-11-24 22:18:49 +0000958 {"update", (PyCFunction)set_update, METH_O,
959 update_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +0000960 {NULL, NULL} /* sentinel */
961};
962
963static PyNumberMethods set_as_number = {
964 0, /*nb_add*/
965 (binaryfunc)set_sub, /*nb_subtract*/
966 0, /*nb_multiply*/
967 0, /*nb_divide*/
968 0, /*nb_remainder*/
969 0, /*nb_divmod*/
970 0, /*nb_power*/
971 0, /*nb_negative*/
972 0, /*nb_positive*/
973 0, /*nb_absolute*/
974 0, /*nb_nonzero*/
975 0, /*nb_invert*/
976 0, /*nb_lshift*/
977 0, /*nb_rshift*/
978 (binaryfunc)set_and, /*nb_and*/
979 (binaryfunc)set_xor, /*nb_xor*/
980 (binaryfunc)set_or, /*nb_or*/
981 0, /*nb_coerce*/
982 0, /*nb_int*/
983 0, /*nb_long*/
984 0, /*nb_float*/
985 0, /*nb_oct*/
986 0, /*nb_hex*/
987 0, /*nb_inplace_add*/
988 (binaryfunc)set_isub, /*nb_inplace_subtract*/
989 0, /*nb_inplace_multiply*/
990 0, /*nb_inplace_divide*/
991 0, /*nb_inplace_remainder*/
992 0, /*nb_inplace_power*/
993 0, /*nb_inplace_lshift*/
994 0, /*nb_inplace_rshift*/
995 (binaryfunc)set_iand, /*nb_inplace_and*/
996 (binaryfunc)set_ixor, /*nb_inplace_xor*/
997 (binaryfunc)set_ior, /*nb_inplace_or*/
998};
999
1000PyDoc_STRVAR(set_doc,
1001"set(iterable) --> set object\n\
1002\n\
1003Build an unordered collection.");
1004
1005PyTypeObject PySet_Type = {
1006 PyObject_HEAD_INIT(&PyType_Type)
1007 0, /* ob_size */
1008 "set", /* tp_name */
1009 sizeof(PySetObject), /* tp_basicsize */
1010 0, /* tp_itemsize */
1011 /* methods */
1012 (destructor)set_dealloc, /* tp_dealloc */
1013 (printfunc)set_tp_print, /* tp_print */
1014 0, /* tp_getattr */
1015 0, /* tp_setattr */
1016 (cmpfunc)set_nocmp, /* tp_compare */
1017 (reprfunc)set_repr, /* tp_repr */
1018 &set_as_number, /* tp_as_number */
1019 &set_as_sequence, /* tp_as_sequence */
1020 0, /* tp_as_mapping */
1021 set_nohash, /* tp_hash */
1022 0, /* tp_call */
1023 0, /* tp_str */
1024 PyObject_GenericGetAttr, /* tp_getattro */
1025 0, /* tp_setattro */
1026 0, /* tp_as_buffer */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001027 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES |
Raymond Hettinger691d8052004-05-30 07:26:47 +00001028 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_WEAKREFS, /* tp_flags */
Raymond Hettingera690a992003-11-16 16:17:49 +00001029 set_doc, /* tp_doc */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001030 (traverseproc)set_traverse, /* tp_traverse */
1031 (inquiry)set_tp_clear, /* tp_clear */
Raymond Hettingera690a992003-11-16 16:17:49 +00001032 (richcmpfunc)set_richcompare, /* tp_richcompare */
Raymond Hettinger691d8052004-05-30 07:26:47 +00001033 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
Raymond Hettingera690a992003-11-16 16:17:49 +00001034 (getiterfunc)set_iter, /* tp_iter */
1035 0, /* tp_iternext */
1036 set_methods, /* tp_methods */
1037 0, /* tp_members */
1038 0, /* tp_getset */
1039 0, /* tp_base */
1040 0, /* tp_dict */
1041 0, /* tp_descr_get */
1042 0, /* tp_descr_set */
1043 0, /* tp_dictoffset */
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001044 (initproc)set_init, /* tp_init */
Raymond Hettingera690a992003-11-16 16:17:49 +00001045 PyType_GenericAlloc, /* tp_alloc */
1046 set_new, /* tp_new */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001047 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00001048};
1049
1050/* frozenset object ********************************************************/
1051
1052
1053static PyMethodDef frozenset_methods[] = {
Raymond Hettinger0deab622003-12-13 18:53:18 +00001054 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001055 contains_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001056 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
Raymond Hettingera690a992003-11-16 16:17:49 +00001057 copy_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001058 {"difference", (PyCFunction)set_difference, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00001059 difference_doc},
1060 {"intersection",(PyCFunction)set_intersection, METH_O,
1061 intersection_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001062 {"issubset", (PyCFunction)set_issubset, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00001063 issubset_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001064 {"issuperset", (PyCFunction)set_issuperset, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00001065 issuperset_doc},
1066 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
1067 reduce_doc},
1068 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
1069 symmetric_difference_doc},
1070 {"union", (PyCFunction)set_union, METH_O,
1071 union_doc},
1072 {NULL, NULL} /* sentinel */
1073};
1074
1075static PyNumberMethods frozenset_as_number = {
1076 0, /*nb_add*/
1077 (binaryfunc)set_sub, /*nb_subtract*/
1078 0, /*nb_multiply*/
1079 0, /*nb_divide*/
1080 0, /*nb_remainder*/
1081 0, /*nb_divmod*/
1082 0, /*nb_power*/
1083 0, /*nb_negative*/
1084 0, /*nb_positive*/
1085 0, /*nb_absolute*/
1086 0, /*nb_nonzero*/
1087 0, /*nb_invert*/
1088 0, /*nb_lshift*/
1089 0, /*nb_rshift*/
1090 (binaryfunc)set_and, /*nb_and*/
1091 (binaryfunc)set_xor, /*nb_xor*/
1092 (binaryfunc)set_or, /*nb_or*/
1093};
1094
1095PyDoc_STRVAR(frozenset_doc,
1096"frozenset(iterable) --> frozenset object\n\
1097\n\
1098Build an immutable unordered collection.");
1099
1100PyTypeObject PyFrozenSet_Type = {
1101 PyObject_HEAD_INIT(&PyType_Type)
1102 0, /* ob_size */
1103 "frozenset", /* tp_name */
1104 sizeof(PySetObject), /* tp_basicsize */
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001105 0, /* tp_itemsize */
1106 /* methods */
Raymond Hettingera690a992003-11-16 16:17:49 +00001107 (destructor)set_dealloc, /* tp_dealloc */
1108 (printfunc)set_tp_print, /* tp_print */
1109 0, /* tp_getattr */
1110 0, /* tp_setattr */
1111 (cmpfunc)set_nocmp, /* tp_compare */
1112 (reprfunc)set_repr, /* tp_repr */
1113 &frozenset_as_number, /* tp_as_number */
1114 &set_as_sequence, /* tp_as_sequence */
1115 0, /* tp_as_mapping */
1116 frozenset_hash, /* tp_hash */
1117 0, /* tp_call */
1118 0, /* tp_str */
1119 PyObject_GenericGetAttr, /* tp_getattro */
1120 0, /* tp_setattro */
1121 0, /* tp_as_buffer */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001122 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES |
Raymond Hettinger691d8052004-05-30 07:26:47 +00001123 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_WEAKREFS, /* tp_flags */
Raymond Hettingera690a992003-11-16 16:17:49 +00001124 frozenset_doc, /* tp_doc */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001125 (traverseproc)set_traverse, /* tp_traverse */
Raymond Hettingera690a992003-11-16 16:17:49 +00001126 0, /* tp_clear */
1127 (richcmpfunc)set_richcompare, /* tp_richcompare */
Raymond Hettinger691d8052004-05-30 07:26:47 +00001128 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
Raymond Hettingera690a992003-11-16 16:17:49 +00001129 (getiterfunc)set_iter, /* tp_iter */
1130 0, /* tp_iternext */
1131 frozenset_methods, /* tp_methods */
1132 0, /* tp_members */
1133 0, /* tp_getset */
1134 0, /* tp_base */
1135 0, /* tp_dict */
1136 0, /* tp_descr_get */
1137 0, /* tp_descr_set */
1138 0, /* tp_dictoffset */
1139 0, /* tp_init */
1140 PyType_GenericAlloc, /* tp_alloc */
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001141 frozenset_new, /* tp_new */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001142 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00001143};