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