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