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