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