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