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