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