blob: 8ef671e3aff6a6b6f4e96b752cc6c37715329845 [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 Hettinger691d8052004-05-30 07:26:47 +0000117 if (so->weakreflist != NULL)
118 PyObject_ClearWeakRefs((PyObject *) so);
Raymond Hettingera690a992003-11-16 16:17:49 +0000119 Py_XDECREF(so->data);
120 so->ob_type->tp_free(so);
121}
122
Raymond Hettingera690a992003-11-16 16:17:49 +0000123static PyObject *
124set_iter(PySetObject *so)
125{
126 return PyObject_GetIter(so->data);
127}
128
129static int
130set_len(PySetObject *so)
131{
132 return PyDict_Size(so->data);
133}
134
135static int
136set_contains(PySetObject *so, PyObject *key)
137{
Raymond Hettingerbfd334a2003-11-22 03:55:23 +0000138 PyObject *tmp;
Raymond Hettinger19c2d772003-11-21 18:36:54 +0000139 int result;
140
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +0000141 result = PyDict_Contains(so->data, key);
Raymond Hettingera38123e2003-11-24 22:18:49 +0000142 if (result == -1 && PyAnySet_Check(key)) {
Raymond Hettinger19c2d772003-11-21 18:36:54 +0000143 PyErr_Clear();
Raymond Hettingerbfd334a2003-11-22 03:55:23 +0000144 tmp = frozenset_dict_wrapper(((PySetObject *)(key))->data);
Raymond Hettinger19c2d772003-11-21 18:36:54 +0000145 if (tmp == NULL)
146 return -1;
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +0000147 result = PyDict_Contains(so->data, tmp);
Raymond Hettinger19c2d772003-11-21 18:36:54 +0000148 Py_DECREF(tmp);
149 }
150 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +0000151}
152
153static PyObject *
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +0000154set_direct_contains(PySetObject *so, PyObject *key)
155{
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +0000156 long result;
157
Raymond Hettinger438e02d2003-12-13 19:38:47 +0000158 result = set_contains(so, key);
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +0000159 if (result == -1)
160 return NULL;
161 return PyBool_FromLong(result);
162}
163
164PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
165
166static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +0000167set_copy(PySetObject *so)
168{
Raymond Hettingera38123e2003-11-24 22:18:49 +0000169 return make_new_set(so->ob_type, (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +0000170}
171
Raymond Hettinger49ba4c32003-11-23 02:49:05 +0000172static PyObject *
173frozenset_copy(PySetObject *so)
174{
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000175 if (PyFrozenSet_CheckExact(so)) {
Raymond Hettinger49ba4c32003-11-23 02:49:05 +0000176 Py_INCREF(so);
177 return (PyObject *)so;
178 }
179 return set_copy(so);
180}
181
Raymond Hettingera690a992003-11-16 16:17:49 +0000182PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
183
184static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000185set_union(PySetObject *so, PyObject *other)
186{
187 PySetObject *result;
188 PyObject *rv;
189
190 result = (PySetObject *)set_copy(so);
191 if (result == NULL)
192 return NULL;
Raymond Hettingera38123e2003-11-24 22:18:49 +0000193 rv = set_update(result, other);
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000194 if (rv == NULL) {
195 Py_DECREF(result);
196 return NULL;
197 }
198 Py_DECREF(rv);
199 return (PyObject *)result;
200}
201
202PyDoc_STRVAR(union_doc,
203 "Return the union of two sets as a new set.\n\
204\n\
205(i.e. all elements that are in either set.)");
206
207static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +0000208set_or(PySetObject *so, PyObject *other)
209{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000210 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000211 Py_INCREF(Py_NotImplemented);
212 return Py_NotImplemented;
213 }
214 return set_union(so, other);
215}
216
217static PyObject *
218set_ior(PySetObject *so, PyObject *other)
219{
220 PyObject *result;
221
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000222 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000223 Py_INCREF(Py_NotImplemented);
224 return Py_NotImplemented;
225 }
Raymond Hettingera38123e2003-11-24 22:18:49 +0000226 result = set_update(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +0000227 if (result == NULL)
228 return NULL;
229 Py_DECREF(result);
230 Py_INCREF(so);
231 return (PyObject *)so;
232}
233
234static PyObject *
235set_intersection(PySetObject *so, PyObject *other)
236{
237 PySetObject *result;
Raymond Hettingera3b11e72003-12-31 14:08:58 +0000238 PyObject *item, *selfdata, *tgtdata, *it, *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +0000239
240 result = (PySetObject *)make_new_set(so->ob_type, NULL);
241 if (result == NULL)
242 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000243 tgtdata = result->data;
244 selfdata = so->data;
245
Raymond Hettingera3b11e72003-12-31 14:08:58 +0000246 if (PyAnySet_Check(other))
247 other = ((PySetObject *)other)->data;
248
249 if (PyDict_Check(other) && PyDict_Size(other) > PyDict_Size(selfdata)) {
250 tmp = selfdata;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000251 selfdata = other;
Raymond Hettingera3b11e72003-12-31 14:08:58 +0000252 other = tmp;
253 }
254
255 if (PyDict_CheckExact(other)) {
256 PyObject *value;
257 int pos = 0;
258 while (PyDict_Next(other, &pos, &item, &value)) {
259 if (PyDict_Contains(selfdata, item)) {
260 if (PyDict_SetItem(tgtdata, item, Py_True) == -1) {
261 Py_DECREF(result);
262 return NULL;
263 }
264 }
265 }
266 return (PyObject *)result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000267 }
268
Raymond Hettingera690a992003-11-16 16:17:49 +0000269 it = PyObject_GetIter(other);
270 if (it == NULL) {
271 Py_DECREF(result);
272 return NULL;
273 }
274
Raymond Hettingera690a992003-11-16 16:17:49 +0000275 while ((item = PyIter_Next(it)) != NULL) {
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +0000276 if (PyDict_Contains(selfdata, item)) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000277 if (PyDict_SetItem(tgtdata, item, Py_True) == -1) {
278 Py_DECREF(it);
279 Py_DECREF(result);
280 Py_DECREF(item);
Raymond Hettingera690a992003-11-16 16:17:49 +0000281 return NULL;
282 }
283 }
284 Py_DECREF(item);
285 }
286 Py_DECREF(it);
287 if (PyErr_Occurred()) {
288 Py_DECREF(result);
289 return NULL;
290 }
291 return (PyObject *)result;
292}
293
294PyDoc_STRVAR(intersection_doc,
295"Return the intersection of two sets as a new set.\n\
296\n\
297(i.e. all elements that are in both sets.)");
298
299static PyObject *
300set_intersection_update(PySetObject *so, PyObject *other)
301{
302 PyObject *item, *selfdata, *it, *newdict, *tmp;
303
304 newdict = PyDict_New();
305 if (newdict == NULL)
306 return newdict;
307
308 it = PyObject_GetIter(other);
309 if (it == NULL) {
310 Py_DECREF(newdict);
311 return NULL;
312 }
313
314 selfdata = so->data;
315 while ((item = PyIter_Next(it)) != NULL) {
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +0000316 if (PyDict_Contains(selfdata, item)) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000317 if (PyDict_SetItem(newdict, item, Py_True) == -1) {
318 Py_DECREF(newdict);
319 Py_DECREF(it);
320 Py_DECREF(item);
Raymond Hettingera690a992003-11-16 16:17:49 +0000321 return NULL;
322 }
323 }
324 Py_DECREF(item);
325 }
326 Py_DECREF(it);
327 if (PyErr_Occurred()) {
328 Py_DECREF(newdict);
329 return NULL;
330 }
331 tmp = so->data;
332 so->data = newdict;
333 Py_DECREF(tmp);
334 Py_RETURN_NONE;
335}
336
337PyDoc_STRVAR(intersection_update_doc,
338"Update a set with the intersection of itself and another.");
339
340static PyObject *
341set_and(PySetObject *so, PyObject *other)
342{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000343 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000344 Py_INCREF(Py_NotImplemented);
345 return Py_NotImplemented;
346 }
347 return set_intersection(so, other);
348}
349
350static PyObject *
351set_iand(PySetObject *so, PyObject *other)
352{
353 PyObject *result;
354
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000355 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000356 Py_INCREF(Py_NotImplemented);
357 return Py_NotImplemented;
358 }
359 result = set_intersection_update(so, other);
360 if (result == NULL)
361 return NULL;
362 Py_DECREF(result);
363 Py_INCREF(so);
364 return (PyObject *)so;
365}
366
367static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +0000368set_difference_update(PySetObject *so, PyObject *other)
369{
370 PyObject *item, *tgtdata, *it;
371
372 it = PyObject_GetIter(other);
373 if (it == NULL)
374 return NULL;
375
376 tgtdata = so->data;
377 while ((item = PyIter_Next(it)) != NULL) {
378 if (PyDict_DelItem(tgtdata, item) == -1) {
379 if (PyErr_ExceptionMatches(PyExc_KeyError))
380 PyErr_Clear();
381 else {
382 Py_DECREF(it);
383 Py_DECREF(item);
384 return NULL;
385 }
386 }
387 Py_DECREF(item);
388 }
389 Py_DECREF(it);
390 if (PyErr_Occurred())
391 return NULL;
392 Py_RETURN_NONE;
393}
394
395PyDoc_STRVAR(difference_update_doc,
396"Remove all elements of another set from this set.");
397
398static PyObject *
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +0000399set_difference(PySetObject *so, PyObject *other)
400{
401 PyObject *result, *tmp;
402 PyObject *otherdata, *tgtdata;
403 PyObject *key, *value;
404 int pos = 0;
405
406 if (PyDict_Check(other))
407 otherdata = other;
408 else if (PyAnySet_Check(other))
409 otherdata = ((PySetObject *)other)->data;
410 else {
411 result = set_copy(so);
412 if (result == NULL)
413 return result;
414 tmp = set_difference_update((PySetObject *)result, other);
415 if (tmp != NULL) {
416 Py_DECREF(tmp);
417 return result;
418 }
419 Py_DECREF(result);
420 return NULL;
421 }
422
423 result = make_new_set(so->ob_type, NULL);
424 if (result == NULL)
425 return NULL;
426 tgtdata = ((PySetObject *)result)->data;
427
428 while (PyDict_Next(so->data, &pos, &key, &value)) {
429 if (!PyDict_Contains(otherdata, key)) {
430 if (PyDict_SetItem(tgtdata, key, Py_True) == -1)
431 return NULL;
432 }
433 }
434 return result;
435}
436
437PyDoc_STRVAR(difference_doc,
438"Return the difference of two sets as a new set.\n\
439\n\
440(i.e. all elements that are in this set but not the other.)");
441static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +0000442set_sub(PySetObject *so, PyObject *other)
443{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000444 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000445 Py_INCREF(Py_NotImplemented);
446 return Py_NotImplemented;
447 }
448 return set_difference(so, other);
449}
450
451static PyObject *
452set_isub(PySetObject *so, PyObject *other)
453{
454 PyObject *result;
455
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000456 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000457 Py_INCREF(Py_NotImplemented);
458 return Py_NotImplemented;
459 }
460 result = set_difference_update(so, other);
461 if (result == NULL)
462 return NULL;
463 Py_DECREF(result);
464 Py_INCREF(so);
465 return (PyObject *)so;
466}
467
468static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +0000469set_symmetric_difference_update(PySetObject *so, PyObject *other)
470{
Raymond Hettingerdc5ae112003-12-13 14:46:46 +0000471 PyObject *selfdata, *otherdata;
Raymond Hettingera690a992003-11-16 16:17:49 +0000472 PySetObject *otherset = NULL;
Raymond Hettingerdc5ae112003-12-13 14:46:46 +0000473 PyObject *key, *value;
474 int pos = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +0000475
476 selfdata = so->data;
Raymond Hettingera690a992003-11-16 16:17:49 +0000477 if (PyDict_Check(other))
478 otherdata = other;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000479 else if (PyAnySet_Check(other))
Raymond Hettingera690a992003-11-16 16:17:49 +0000480 otherdata = ((PySetObject *)other)->data;
481 else {
482 otherset = (PySetObject *)make_new_set(so->ob_type, other);
483 if (otherset == NULL)
484 return NULL;
485 otherdata = otherset->data;
486 }
487
Raymond Hettingerdc5ae112003-12-13 14:46:46 +0000488 while (PyDict_Next(otherdata, &pos, &key, &value)) {
489 if (PyDict_Contains(selfdata, key)) {
490 if (PyDict_DelItem(selfdata, key) == -1) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000491 Py_XDECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +0000492 return NULL;
493 }
494 } else {
Raymond Hettingerdc5ae112003-12-13 14:46:46 +0000495 if (PyDict_SetItem(selfdata, key, Py_True) == -1) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000496 Py_XDECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +0000497 return NULL;
498 }
499 }
Raymond Hettingera690a992003-11-16 16:17:49 +0000500 }
501 Py_XDECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +0000502 Py_RETURN_NONE;
503}
504
505PyDoc_STRVAR(symmetric_difference_update_doc,
506"Update a set with the symmetric difference of itself and another.");
507
508static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000509set_symmetric_difference(PySetObject *so, PyObject *other)
510{
511 PySetObject *result;
Raymond Hettingerdc5ae112003-12-13 14:46:46 +0000512 PyObject *selfdata, *otherdata, *tgtdata, *rv, *otherset;
513 PyObject *key, *value;
514 int pos = 0;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000515
516 if (PyDict_Check(other))
517 otherdata = other;
518 else if (PyAnySet_Check(other))
519 otherdata = ((PySetObject *)other)->data;
520 else {
521 otherset = make_new_set(so->ob_type, other);
522 if (otherset == NULL)
523 return NULL;
524 rv = set_symmetric_difference_update((PySetObject *)otherset, (PyObject *)so);
525 if (rv == NULL)
526 return NULL;
527 Py_DECREF(rv);
528 return otherset;
529 }
530
531 result = (PySetObject *)make_new_set(so->ob_type, NULL);
532 if (result == NULL)
533 return NULL;
534 tgtdata = result->data;
535 selfdata = so->data;
536
Raymond Hettingerdc5ae112003-12-13 14:46:46 +0000537 while (PyDict_Next(otherdata, &pos, &key, &value)) {
538 if (!PyDict_Contains(selfdata, key)) {
539 if (PyDict_SetItem(tgtdata, key, Py_True) == -1) {
540 Py_DECREF(result);
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000541 return NULL;
542 }
543 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000544 }
545
Raymond Hettingerdc5ae112003-12-13 14:46:46 +0000546 pos = 0;
547 while (PyDict_Next(selfdata, &pos, &key, &value)) {
548 if (!PyDict_Contains(otherdata, key)) {
549 if (PyDict_SetItem(tgtdata, key, Py_True) == -1) {
550 Py_DECREF(result);
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000551 return NULL;
552 }
553 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000554 }
555
556 return (PyObject *)result;
557}
558
559PyDoc_STRVAR(symmetric_difference_doc,
560"Return the symmetric difference of two sets as a new set.\n\
561\n\
562(i.e. all elements that are in exactly one of the sets.)");
563
564static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +0000565set_xor(PySetObject *so, PyObject *other)
566{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000567 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000568 Py_INCREF(Py_NotImplemented);
569 return Py_NotImplemented;
570 }
571 return set_symmetric_difference(so, other);
572}
573
574static PyObject *
575set_ixor(PySetObject *so, PyObject *other)
576{
577 PyObject *result;
578
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000579 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000580 Py_INCREF(Py_NotImplemented);
581 return Py_NotImplemented;
582 }
583 result = set_symmetric_difference_update(so, other);
584 if (result == NULL)
585 return NULL;
586 Py_DECREF(result);
587 Py_INCREF(so);
588 return (PyObject *)so;
589}
590
591static PyObject *
592set_issubset(PySetObject *so, PyObject *other)
593{
Raymond Hettingerdc5ae112003-12-13 14:46:46 +0000594 PyObject *otherdata, *tmp, *result;
595 PyObject *key, *value;
596 int pos = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +0000597
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000598 if (!PyAnySet_Check(other)) {
Raymond Hettinger3fbec702003-11-21 07:56:36 +0000599 tmp = make_new_set(&PySet_Type, other);
600 if (tmp == NULL)
601 return NULL;
602 result = set_issubset(so, tmp);
603 Py_DECREF(tmp);
604 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +0000605 }
606 if (set_len(so) > set_len((PySetObject *)other))
607 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +0000608
609 otherdata = ((PySetObject *)other)->data;
Raymond Hettingerdc5ae112003-12-13 14:46:46 +0000610 while (PyDict_Next(((PySetObject *)so)->data, &pos, &key, &value)) {
611 if (!PyDict_Contains(otherdata, key))
Raymond Hettingera690a992003-11-16 16:17:49 +0000612 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +0000613 }
Raymond Hettingera690a992003-11-16 16:17:49 +0000614 Py_RETURN_TRUE;
615}
616
617PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
618
619static PyObject *
620set_issuperset(PySetObject *so, PyObject *other)
621{
Raymond Hettinger3fbec702003-11-21 07:56:36 +0000622 PyObject *tmp, *result;
623
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000624 if (!PyAnySet_Check(other)) {
Raymond Hettinger3fbec702003-11-21 07:56:36 +0000625 tmp = make_new_set(&PySet_Type, other);
626 if (tmp == NULL)
627 return NULL;
628 result = set_issuperset(so, tmp);
629 Py_DECREF(tmp);
630 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +0000631 }
632 return set_issubset((PySetObject *)other, (PyObject *)so);
633}
634
635PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
636
637static long
638set_nohash(PyObject *self)
639{
640 PyErr_SetString(PyExc_TypeError, "set objects are unhashable");
641 return -1;
642}
643
644static int
645set_nocmp(PyObject *self)
646{
647 PyErr_SetString(PyExc_TypeError, "cannot compare sets using cmp()");
648 return -1;
649}
650
651static long
652frozenset_hash(PyObject *self)
653{
Raymond Hettingera690a992003-11-16 16:17:49 +0000654 PySetObject *so = (PySetObject *)self;
Raymond Hettingerdc5ae112003-12-13 14:46:46 +0000655 PyObject *key, *value;
656 int pos = 0;
Raymond Hettingerc9786332004-06-10 22:41:48 +0000657 long hash = 1927868237L;
Raymond Hettinger438e02d2003-12-13 19:38:47 +0000658
Raymond Hettingera690a992003-11-16 16:17:49 +0000659 if (so->hash != -1)
660 return so->hash;
Raymond Hettingera690a992003-11-16 16:17:49 +0000661
Raymond Hettingerc9786332004-06-10 22:41:48 +0000662 hash *= (PyDict_Size(so->data) + 1);
Raymond Hettingerdc5ae112003-12-13 14:46:46 +0000663 while (PyDict_Next(so->data, &pos, &key, &value)) {
Raymond Hettingerc9786332004-06-10 22:41:48 +0000664 /* Work to increase the bit dispersion for closely spaced hash
665 values. The is important because some use cases have many
666 combinations of a small number of elements with nearby
667 hashes so that many distinct combinations collapse to only
668 a handful of distinct hash values. */
669 long h = PyObject_Hash(key);
670 hash ^= (h ^ (h << 16) ^ 89869747L) * 3644798167u;
Raymond Hettingera690a992003-11-16 16:17:49 +0000671 }
Raymond Hettingerc9786332004-06-10 22:41:48 +0000672 hash = hash * 69069L + 907133923L;
Raymond Hettinger27e403e2004-06-10 21:38:41 +0000673 if (hash == -1)
674 hash = 590923713L;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000675 so->hash = hash;
Raymond Hettingera690a992003-11-16 16:17:49 +0000676 return hash;
677}
678
679static PyObject *
680set_richcompare(PySetObject *v, PyObject *w, int op)
681{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000682 if(!PyAnySet_Check(w)) {
683 if (op == Py_EQ)
684 Py_RETURN_FALSE;
685 if (op == Py_NE)
686 Py_RETURN_TRUE;
Raymond Hettingera690a992003-11-16 16:17:49 +0000687 PyErr_SetString(PyExc_TypeError, "can only compare to a set");
688 return NULL;
689 }
690 switch (op) {
691 case Py_EQ:
692 case Py_NE:
693 return PyObject_RichCompare(((PySetObject *)v)->data,
694 ((PySetObject *)w)->data, op);
695 case Py_LE:
696 return set_issubset((PySetObject *)v, w);
697 case Py_GE:
698 return set_issuperset((PySetObject *)v, w);
699 case Py_LT:
700 if (set_len(v) >= set_len((PySetObject *)w))
701 Py_RETURN_FALSE;
702 return set_issubset((PySetObject *)v, w);
703 case Py_GT:
704 if (set_len(v) <= set_len((PySetObject *)w))
705 Py_RETURN_FALSE;
706 return set_issuperset((PySetObject *)v, w);
707 }
708 Py_INCREF(Py_NotImplemented);
709 return Py_NotImplemented;
710}
711
712static PyObject *
713set_repr(PySetObject *so)
714{
715 PyObject *keys, *result, *listrepr;
716
717 keys = PyDict_Keys(so->data);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000718 if (keys == NULL)
719 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +0000720 listrepr = PyObject_Repr(keys);
721 Py_DECREF(keys);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000722 if (listrepr == NULL)
723 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +0000724
725 result = PyString_FromFormat("%s(%s)", so->ob_type->tp_name,
726 PyString_AS_STRING(listrepr));
727 Py_DECREF(listrepr);
728 return result;
729}
730
731static int
732set_tp_print(PySetObject *so, FILE *fp, int flags)
733{
Raymond Hettingerdc5ae112003-12-13 14:46:46 +0000734 PyObject *key, *value;
Raymond Hettingera3b11e72003-12-31 14:08:58 +0000735 int pos=0;
736 char *emit = ""; /* No separator emitted on first pass */
737 char *separator = ", ";
Raymond Hettingera690a992003-11-16 16:17:49 +0000738
Raymond Hettingera690a992003-11-16 16:17:49 +0000739 fprintf(fp, "%s([", so->ob_type->tp_name);
Raymond Hettingerdc5ae112003-12-13 14:46:46 +0000740 while (PyDict_Next(so->data, &pos, &key, &value)) {
Raymond Hettingera3b11e72003-12-31 14:08:58 +0000741 fputs(emit, fp);
742 emit = separator;
Raymond Hettingerdc5ae112003-12-13 14:46:46 +0000743 if (PyObject_Print(key, fp, 0) != 0)
Raymond Hettingera690a992003-11-16 16:17:49 +0000744 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000745 }
Raymond Hettingera3b11e72003-12-31 14:08:58 +0000746 fputs("])", fp);
Raymond Hettingera690a992003-11-16 16:17:49 +0000747 return 0;
748}
749
750static PyObject *
751set_clear(PySetObject *so)
752{
753 PyDict_Clear(so->data);
754 so->hash = -1;
Raymond Hettinger438e02d2003-12-13 19:38:47 +0000755 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +0000756}
757
758PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
759
Raymond Hettingera690a992003-11-16 16:17:49 +0000760static PyObject *
761set_add(PySetObject *so, PyObject *item)
762{
763 if (PyDict_SetItem(so->data, item, Py_True) == -1)
764 return NULL;
Raymond Hettinger438e02d2003-12-13 19:38:47 +0000765 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +0000766}
767
768PyDoc_STRVAR(add_doc,
769"Add an element to a set.\n\
770\n\
771This has no effect if the element is already present.");
772
773static PyObject *
774set_remove(PySetObject *so, PyObject *item)
775{
Raymond Hettinger0deab622003-12-13 18:53:18 +0000776 PyObject *tmp, *result;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +0000777
Raymond Hettinger0deab622003-12-13 18:53:18 +0000778 if (PyType_IsSubtype(item->ob_type, &PySet_Type)) {
Raymond Hettingerbfd334a2003-11-22 03:55:23 +0000779 tmp = frozenset_dict_wrapper(((PySetObject *)(item))->data);
780 if (tmp == NULL)
781 return NULL;
Raymond Hettinger0deab622003-12-13 18:53:18 +0000782 result = set_remove(so, tmp);
Raymond Hettingerbfd334a2003-11-22 03:55:23 +0000783 Py_DECREF(tmp);
Raymond Hettinger0deab622003-12-13 18:53:18 +0000784 return result;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +0000785 }
Raymond Hettinger0deab622003-12-13 18:53:18 +0000786
787 if (PyDict_DelItem(so->data, item) == -1)
788 return NULL;
Raymond Hettinger438e02d2003-12-13 19:38:47 +0000789 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +0000790}
791
792PyDoc_STRVAR(remove_doc,
793"Remove an element from a set; it must be a member.\n\
794\n\
795If the element is not a member, raise a KeyError.");
796
797static PyObject *
798set_discard(PySetObject *so, PyObject *item)
799{
Raymond Hettinger0deab622003-12-13 18:53:18 +0000800 PyObject *tmp, *result;
801
802 if (PyType_IsSubtype(item->ob_type, &PySet_Type)) {
803 tmp = frozenset_dict_wrapper(((PySetObject *)(item))->data);
804 if (tmp == NULL)
805 return NULL;
806 result = set_discard(so, tmp);
807 Py_DECREF(tmp);
808 return result;
809 }
Raymond Hettingerbfd334a2003-11-22 03:55:23 +0000810
Guido van Rossumb61982b2003-11-18 19:27:19 +0000811 if (PyDict_DelItem(so->data, item) == -1) {
Raymond Hettinger0deab622003-12-13 18:53:18 +0000812 if (!PyErr_ExceptionMatches(PyExc_KeyError))
813 return NULL;
814 PyErr_Clear();
Guido van Rossumb61982b2003-11-18 19:27:19 +0000815 }
Raymond Hettinger438e02d2003-12-13 19:38:47 +0000816 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +0000817}
818
819PyDoc_STRVAR(discard_doc,
820"Remove an element from a set if it is a member.\n\
821\n\
822If the element is not a member, do nothing.");
823
824static PyObject *
825set_pop(PySetObject *so)
826{
827 PyObject *key, *value;
828 int pos = 0;
829
830 if (!PyDict_Next(so->data, &pos, &key, &value)) {
831 PyErr_SetString(PyExc_KeyError, "pop from an empty set");
832 return NULL;
833 }
834 Py_INCREF(key);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000835 if (PyDict_DelItem(so->data, key) == -1) {
836 Py_DECREF(key);
837 return NULL;
838 }
Raymond Hettingera690a992003-11-16 16:17:49 +0000839 return key;
840}
841
842PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.");
843
844static PyObject *
845set_reduce(PySetObject *so)
846{
Raymond Hettinger15056a52004-11-09 07:25:31 +0000847 PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +0000848
849 keys = PyDict_Keys(so->data);
850 if (keys == NULL)
851 goto done;
852 args = PyTuple_Pack(1, keys);
853 if (args == NULL)
854 goto done;
Raymond Hettinger15056a52004-11-09 07:25:31 +0000855 dict = PyObject_GetAttrString((PyObject *)so, "__dict__");
856 if (dict == NULL) {
857 PyErr_Clear();
858 dict = Py_None;
859 Py_INCREF(dict);
860 }
861 result = PyTuple_Pack(3, so->ob_type, args, dict);
Raymond Hettingera690a992003-11-16 16:17:49 +0000862done:
863 Py_XDECREF(args);
864 Py_XDECREF(keys);
Raymond Hettinger15056a52004-11-09 07:25:31 +0000865 Py_XDECREF(dict);
Raymond Hettingera690a992003-11-16 16:17:49 +0000866 return result;
867}
868
869PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
870
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000871static int
872set_init(PySetObject *self, PyObject *args, PyObject *kwds)
873{
874 PyObject *iterable = NULL;
875 PyObject *result;
876
877 if (!PyAnySet_Check(self))
878 return -1;
879 if (!PyArg_UnpackTuple(args, self->ob_type->tp_name, 0, 1, &iterable))
880 return -1;
881 PyDict_Clear(self->data);
882 self->hash = -1;
883 if (iterable == NULL)
884 return 0;
Raymond Hettingera38123e2003-11-24 22:18:49 +0000885 result = set_update(self, iterable);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000886 if (result != NULL) {
887 Py_DECREF(result);
888 return 0;
889 }
890 return -1;
891}
892
Raymond Hettingera690a992003-11-16 16:17:49 +0000893static PySequenceMethods set_as_sequence = {
894 (inquiry)set_len, /* sq_length */
895 0, /* sq_concat */
896 0, /* sq_repeat */
897 0, /* sq_item */
898 0, /* sq_slice */
899 0, /* sq_ass_item */
900 0, /* sq_ass_slice */
901 (objobjproc)set_contains, /* sq_contains */
902};
903
904/* set object ********************************************************/
905
906static PyMethodDef set_methods[] = {
907 {"add", (PyCFunction)set_add, METH_O,
908 add_doc},
909 {"clear", (PyCFunction)set_clear, METH_NOARGS,
910 clear_doc},
Raymond Hettinger0deab622003-12-13 18:53:18 +0000911 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +0000912 contains_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +0000913 {"copy", (PyCFunction)set_copy, METH_NOARGS,
914 copy_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +0000915 {"discard", (PyCFunction)set_discard, METH_O,
916 discard_doc},
917 {"difference", (PyCFunction)set_difference, METH_O,
918 difference_doc},
919 {"difference_update", (PyCFunction)set_difference_update, METH_O,
920 difference_update_doc},
921 {"intersection",(PyCFunction)set_intersection, METH_O,
922 intersection_doc},
923 {"intersection_update",(PyCFunction)set_intersection_update, METH_O,
924 intersection_update_doc},
925 {"issubset", (PyCFunction)set_issubset, METH_O,
926 issubset_doc},
927 {"issuperset", (PyCFunction)set_issuperset, METH_O,
928 issuperset_doc},
929 {"pop", (PyCFunction)set_pop, METH_NOARGS,
930 pop_doc},
931 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
932 reduce_doc},
933 {"remove", (PyCFunction)set_remove, METH_O,
934 remove_doc},
935 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
936 symmetric_difference_doc},
937 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
938 symmetric_difference_update_doc},
939 {"union", (PyCFunction)set_union, METH_O,
940 union_doc},
Raymond Hettingera38123e2003-11-24 22:18:49 +0000941 {"update", (PyCFunction)set_update, METH_O,
942 update_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +0000943 {NULL, NULL} /* sentinel */
944};
945
946static PyNumberMethods set_as_number = {
947 0, /*nb_add*/
948 (binaryfunc)set_sub, /*nb_subtract*/
949 0, /*nb_multiply*/
950 0, /*nb_divide*/
951 0, /*nb_remainder*/
952 0, /*nb_divmod*/
953 0, /*nb_power*/
954 0, /*nb_negative*/
955 0, /*nb_positive*/
956 0, /*nb_absolute*/
957 0, /*nb_nonzero*/
958 0, /*nb_invert*/
959 0, /*nb_lshift*/
960 0, /*nb_rshift*/
961 (binaryfunc)set_and, /*nb_and*/
962 (binaryfunc)set_xor, /*nb_xor*/
963 (binaryfunc)set_or, /*nb_or*/
964 0, /*nb_coerce*/
965 0, /*nb_int*/
966 0, /*nb_long*/
967 0, /*nb_float*/
968 0, /*nb_oct*/
969 0, /*nb_hex*/
970 0, /*nb_inplace_add*/
971 (binaryfunc)set_isub, /*nb_inplace_subtract*/
972 0, /*nb_inplace_multiply*/
973 0, /*nb_inplace_divide*/
974 0, /*nb_inplace_remainder*/
975 0, /*nb_inplace_power*/
976 0, /*nb_inplace_lshift*/
977 0, /*nb_inplace_rshift*/
978 (binaryfunc)set_iand, /*nb_inplace_and*/
979 (binaryfunc)set_ixor, /*nb_inplace_xor*/
980 (binaryfunc)set_ior, /*nb_inplace_or*/
981};
982
983PyDoc_STRVAR(set_doc,
984"set(iterable) --> set object\n\
985\n\
986Build an unordered collection.");
987
988PyTypeObject PySet_Type = {
989 PyObject_HEAD_INIT(&PyType_Type)
990 0, /* ob_size */
991 "set", /* tp_name */
992 sizeof(PySetObject), /* tp_basicsize */
993 0, /* tp_itemsize */
994 /* methods */
995 (destructor)set_dealloc, /* tp_dealloc */
996 (printfunc)set_tp_print, /* tp_print */
997 0, /* tp_getattr */
998 0, /* tp_setattr */
999 (cmpfunc)set_nocmp, /* tp_compare */
1000 (reprfunc)set_repr, /* tp_repr */
1001 &set_as_number, /* tp_as_number */
1002 &set_as_sequence, /* tp_as_sequence */
1003 0, /* tp_as_mapping */
1004 set_nohash, /* tp_hash */
1005 0, /* tp_call */
1006 0, /* tp_str */
1007 PyObject_GenericGetAttr, /* tp_getattro */
1008 0, /* tp_setattro */
1009 0, /* tp_as_buffer */
Raymond Hettinger47edb4b2004-06-13 08:20:46 +00001010 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES |
Raymond Hettinger691d8052004-05-30 07:26:47 +00001011 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_WEAKREFS, /* tp_flags */
Raymond Hettingera690a992003-11-16 16:17:49 +00001012 set_doc, /* tp_doc */
Raymond Hettinger47edb4b2004-06-13 08:20:46 +00001013 0, /* tp_traverse */
1014 0, /* tp_clear */
Raymond Hettingera690a992003-11-16 16:17:49 +00001015 (richcmpfunc)set_richcompare, /* tp_richcompare */
Raymond Hettinger691d8052004-05-30 07:26:47 +00001016 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
Raymond Hettingera690a992003-11-16 16:17:49 +00001017 (getiterfunc)set_iter, /* tp_iter */
1018 0, /* tp_iternext */
1019 set_methods, /* tp_methods */
1020 0, /* tp_members */
1021 0, /* tp_getset */
1022 0, /* tp_base */
1023 0, /* tp_dict */
1024 0, /* tp_descr_get */
1025 0, /* tp_descr_set */
1026 0, /* tp_dictoffset */
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001027 (initproc)set_init, /* tp_init */
Raymond Hettingera690a992003-11-16 16:17:49 +00001028 PyType_GenericAlloc, /* tp_alloc */
1029 set_new, /* tp_new */
Raymond Hettinger47edb4b2004-06-13 08:20:46 +00001030 PyObject_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00001031};
1032
1033/* frozenset object ********************************************************/
1034
1035
1036static PyMethodDef frozenset_methods[] = {
Raymond Hettinger0deab622003-12-13 18:53:18 +00001037 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001038 contains_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001039 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
Raymond Hettingera690a992003-11-16 16:17:49 +00001040 copy_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001041 {"difference", (PyCFunction)set_difference, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00001042 difference_doc},
1043 {"intersection",(PyCFunction)set_intersection, METH_O,
1044 intersection_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001045 {"issubset", (PyCFunction)set_issubset, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00001046 issubset_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001047 {"issuperset", (PyCFunction)set_issuperset, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00001048 issuperset_doc},
1049 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
1050 reduce_doc},
1051 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
1052 symmetric_difference_doc},
1053 {"union", (PyCFunction)set_union, METH_O,
1054 union_doc},
1055 {NULL, NULL} /* sentinel */
1056};
1057
1058static PyNumberMethods frozenset_as_number = {
1059 0, /*nb_add*/
1060 (binaryfunc)set_sub, /*nb_subtract*/
1061 0, /*nb_multiply*/
1062 0, /*nb_divide*/
1063 0, /*nb_remainder*/
1064 0, /*nb_divmod*/
1065 0, /*nb_power*/
1066 0, /*nb_negative*/
1067 0, /*nb_positive*/
1068 0, /*nb_absolute*/
1069 0, /*nb_nonzero*/
1070 0, /*nb_invert*/
1071 0, /*nb_lshift*/
1072 0, /*nb_rshift*/
1073 (binaryfunc)set_and, /*nb_and*/
1074 (binaryfunc)set_xor, /*nb_xor*/
1075 (binaryfunc)set_or, /*nb_or*/
1076};
1077
1078PyDoc_STRVAR(frozenset_doc,
1079"frozenset(iterable) --> frozenset object\n\
1080\n\
1081Build an immutable unordered collection.");
1082
1083PyTypeObject PyFrozenSet_Type = {
1084 PyObject_HEAD_INIT(&PyType_Type)
1085 0, /* ob_size */
1086 "frozenset", /* tp_name */
1087 sizeof(PySetObject), /* tp_basicsize */
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001088 0, /* tp_itemsize */
1089 /* methods */
Raymond Hettingera690a992003-11-16 16:17:49 +00001090 (destructor)set_dealloc, /* tp_dealloc */
1091 (printfunc)set_tp_print, /* tp_print */
1092 0, /* tp_getattr */
1093 0, /* tp_setattr */
1094 (cmpfunc)set_nocmp, /* tp_compare */
1095 (reprfunc)set_repr, /* tp_repr */
1096 &frozenset_as_number, /* tp_as_number */
1097 &set_as_sequence, /* tp_as_sequence */
1098 0, /* tp_as_mapping */
1099 frozenset_hash, /* tp_hash */
1100 0, /* tp_call */
1101 0, /* tp_str */
1102 PyObject_GenericGetAttr, /* tp_getattro */
1103 0, /* tp_setattro */
1104 0, /* tp_as_buffer */
Raymond Hettinger47edb4b2004-06-13 08:20:46 +00001105 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES |
Raymond Hettinger691d8052004-05-30 07:26:47 +00001106 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_WEAKREFS, /* tp_flags */
Raymond Hettingera690a992003-11-16 16:17:49 +00001107 frozenset_doc, /* tp_doc */
Raymond Hettinger47edb4b2004-06-13 08:20:46 +00001108 0, /* tp_traverse */
Raymond Hettingera690a992003-11-16 16:17:49 +00001109 0, /* tp_clear */
1110 (richcmpfunc)set_richcompare, /* tp_richcompare */
Raymond Hettinger691d8052004-05-30 07:26:47 +00001111 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
Raymond Hettingera690a992003-11-16 16:17:49 +00001112 (getiterfunc)set_iter, /* tp_iter */
1113 0, /* tp_iternext */
1114 frozenset_methods, /* tp_methods */
1115 0, /* tp_members */
1116 0, /* tp_getset */
1117 0, /* tp_base */
1118 0, /* tp_dict */
1119 0, /* tp_descr_get */
1120 0, /* tp_descr_set */
1121 0, /* tp_dictoffset */
1122 0, /* tp_init */
1123 PyType_GenericAlloc, /* tp_alloc */
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001124 frozenset_new, /* tp_new */
Raymond Hettinger47edb4b2004-06-13 08:20:46 +00001125 PyObject_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00001126};