Raymond Hettinger | c991db2 | 2005-08-11 07:58:45 +0000 | [diff] [blame] | 1 | |
Raymond Hettinger | a9d9936 | 2005-08-05 00:01:15 +0000 | [diff] [blame] | 2 | /* set object implementation |
| 3 | Written and maintained by Raymond D. Hettinger <python@rcn.com> |
| 4 | Derived from Lib/sets.py and Objects/dictobject.c. |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 5 | |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 6 | Copyright (c) 2003-2007 Python Software Foundation. |
Raymond Hettinger | a9d9936 | 2005-08-05 00:01:15 +0000 | [diff] [blame] | 7 | All rights reserved. |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 10 | #include "Python.h" |
Raymond Hettinger | a9d9936 | 2005-08-05 00:01:15 +0000 | [diff] [blame] | 11 | #include "structmember.h" |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 12 | |
Raymond Hettinger | 9c14ffb | 2006-12-08 04:57:50 +0000 | [diff] [blame] | 13 | /* Set a key error with the specified argument, wrapping it in a |
| 14 | * tuple automatically so that tuple keys are not unpacked as the |
| 15 | * exception arguments. */ |
| 16 | static void |
| 17 | set_key_error(PyObject *arg) |
| 18 | { |
| 19 | PyObject *tup; |
| 20 | tup = PyTuple_Pack(1, arg); |
| 21 | if (!tup) |
| 22 | return; /* caller will expect error to be set anyway */ |
| 23 | PyErr_SetObject(PyExc_KeyError, tup); |
| 24 | Py_DECREF(tup); |
| 25 | } |
| 26 | |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 27 | /* This must be >= 1. */ |
| 28 | #define PERTURB_SHIFT 5 |
| 29 | |
| 30 | /* Object used as dummy key to fill deleted entries */ |
Raymond Hettinger | a9d9936 | 2005-08-05 00:01:15 +0000 | [diff] [blame] | 31 | static PyObject *dummy = NULL; /* Initialized by first call to make_new_set() */ |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 32 | |
Armin Rigo | e170937 | 2006-04-12 17:06:05 +0000 | [diff] [blame] | 33 | #ifdef Py_REF_DEBUG |
| 34 | PyObject * |
| 35 | _PySet_Dummy(void) |
| 36 | { |
| 37 | return dummy; |
| 38 | } |
| 39 | #endif |
| 40 | |
Raymond Hettinger | bc841a1 | 2005-08-07 13:02:53 +0000 | [diff] [blame] | 41 | #define INIT_NONZERO_SET_SLOTS(so) do { \ |
| 42 | (so)->table = (so)->smalltable; \ |
| 43 | (so)->mask = PySet_MINSIZE - 1; \ |
| 44 | (so)->hash = -1; \ |
| 45 | } while(0) |
| 46 | |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 47 | #define EMPTY_TO_MINSIZE(so) do { \ |
| 48 | memset((so)->smalltable, 0, sizeof((so)->smalltable)); \ |
| 49 | (so)->used = (so)->fill = 0; \ |
Raymond Hettinger | bc841a1 | 2005-08-07 13:02:53 +0000 | [diff] [blame] | 50 | INIT_NONZERO_SET_SLOTS(so); \ |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 51 | } while(0) |
| 52 | |
Raymond Hettinger | bc841a1 | 2005-08-07 13:02:53 +0000 | [diff] [blame] | 53 | /* Reuse scheme to save calls to malloc, free, and memset */ |
| 54 | #define MAXFREESETS 80 |
| 55 | static PySetObject *free_sets[MAXFREESETS]; |
| 56 | static int num_free_sets = 0; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 57 | |
| 58 | /* |
| 59 | The basic lookup function used by all operations. |
| 60 | This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4. |
| 61 | Open addressing is preferred over chaining since the link overhead for |
| 62 | chaining would be substantial (100% with typical malloc overhead). |
| 63 | |
| 64 | The initial probe index is computed as hash mod the table size. Subsequent |
Raymond Hettinger | bc841a1 | 2005-08-07 13:02:53 +0000 | [diff] [blame] | 65 | probe indices are computed as explained in Objects/dictobject.c. |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 66 | |
| 67 | All arithmetic on hash should ignore overflow. |
| 68 | |
Raymond Hettinger | 9bda1d6 | 2005-09-16 07:14:21 +0000 | [diff] [blame] | 69 | Unlike the dictionary implementation, the lookkey functions can return |
| 70 | NULL if the rich comparison returns an error. |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 71 | */ |
| 72 | |
| 73 | static setentry * |
| 74 | set_lookkey(PySetObject *so, PyObject *key, register long hash) |
| 75 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 76 | register Py_ssize_t i; |
| 77 | register size_t perturb; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 78 | register setentry *freeslot; |
Neal Norwitz | 0f2783c | 2006-06-19 05:40:44 +0000 | [diff] [blame] | 79 | register size_t mask = so->mask; |
Raymond Hettinger | a580c47 | 2005-08-05 17:19:54 +0000 | [diff] [blame] | 80 | setentry *table = so->table; |
Raymond Hettinger | 06d8cf8 | 2005-07-31 15:36:06 +0000 | [diff] [blame] | 81 | register setentry *entry; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 82 | register int cmp; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 83 | PyObject *startkey; |
| 84 | |
| 85 | i = hash & mask; |
Raymond Hettinger | a580c47 | 2005-08-05 17:19:54 +0000 | [diff] [blame] | 86 | entry = &table[i]; |
Raymond Hettinger | 06d8cf8 | 2005-07-31 15:36:06 +0000 | [diff] [blame] | 87 | if (entry->key == NULL || entry->key == key) |
| 88 | return entry; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 89 | |
Raymond Hettinger | 06d8cf8 | 2005-07-31 15:36:06 +0000 | [diff] [blame] | 90 | if (entry->key == dummy) |
| 91 | freeslot = entry; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 92 | else { |
Raymond Hettinger | 06d8cf8 | 2005-07-31 15:36:06 +0000 | [diff] [blame] | 93 | if (entry->hash == hash) { |
Raymond Hettinger | 06d8cf8 | 2005-07-31 15:36:06 +0000 | [diff] [blame] | 94 | startkey = entry->key; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 95 | cmp = PyObject_RichCompareBool(startkey, key, Py_EQ); |
| 96 | if (cmp < 0) |
Raymond Hettinger | 9bda1d6 | 2005-09-16 07:14:21 +0000 | [diff] [blame] | 97 | return NULL; |
Raymond Hettinger | a580c47 | 2005-08-05 17:19:54 +0000 | [diff] [blame] | 98 | if (table == so->table && entry->key == startkey) { |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 99 | if (cmp > 0) |
Raymond Hettinger | 9bda1d6 | 2005-09-16 07:14:21 +0000 | [diff] [blame] | 100 | return entry; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 101 | } |
| 102 | else { |
| 103 | /* The compare did major nasty stuff to the |
| 104 | * set: start over. |
| 105 | */ |
Raymond Hettinger | 9bda1d6 | 2005-09-16 07:14:21 +0000 | [diff] [blame] | 106 | return set_lookkey(so, key, hash); |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | freeslot = NULL; |
| 110 | } |
| 111 | |
| 112 | /* In the loop, key == dummy is by far (factor of 100s) the |
| 113 | least likely outcome, so test for that last. */ |
| 114 | for (perturb = hash; ; perturb >>= PERTURB_SHIFT) { |
| 115 | i = (i << 2) + i + perturb + 1; |
Raymond Hettinger | a580c47 | 2005-08-05 17:19:54 +0000 | [diff] [blame] | 116 | entry = &table[i & mask]; |
Raymond Hettinger | 06d8cf8 | 2005-07-31 15:36:06 +0000 | [diff] [blame] | 117 | if (entry->key == NULL) { |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 118 | if (freeslot != NULL) |
Raymond Hettinger | 06d8cf8 | 2005-07-31 15:36:06 +0000 | [diff] [blame] | 119 | entry = freeslot; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 120 | break; |
| 121 | } |
Raymond Hettinger | 06d8cf8 | 2005-07-31 15:36:06 +0000 | [diff] [blame] | 122 | if (entry->key == key) |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 123 | break; |
Raymond Hettinger | 06d8cf8 | 2005-07-31 15:36:06 +0000 | [diff] [blame] | 124 | if (entry->hash == hash && entry->key != dummy) { |
Raymond Hettinger | 06d8cf8 | 2005-07-31 15:36:06 +0000 | [diff] [blame] | 125 | startkey = entry->key; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 126 | cmp = PyObject_RichCompareBool(startkey, key, Py_EQ); |
| 127 | if (cmp < 0) |
Raymond Hettinger | 9bda1d6 | 2005-09-16 07:14:21 +0000 | [diff] [blame] | 128 | return NULL; |
Raymond Hettinger | a580c47 | 2005-08-05 17:19:54 +0000 | [diff] [blame] | 129 | if (table == so->table && entry->key == startkey) { |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 130 | if (cmp > 0) |
| 131 | break; |
| 132 | } |
| 133 | else { |
| 134 | /* The compare did major nasty stuff to the |
| 135 | * set: start over. |
| 136 | */ |
Raymond Hettinger | 9bda1d6 | 2005-09-16 07:14:21 +0000 | [diff] [blame] | 137 | return set_lookkey(so, key, hash); |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 138 | } |
| 139 | } |
Raymond Hettinger | 06d8cf8 | 2005-07-31 15:36:06 +0000 | [diff] [blame] | 140 | else if (entry->key == dummy && freeslot == NULL) |
| 141 | freeslot = entry; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 142 | } |
Raymond Hettinger | 06d8cf8 | 2005-07-31 15:36:06 +0000 | [diff] [blame] | 143 | return entry; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | /* |
| 147 | * Hacked up version of set_lookkey which can assume keys are always strings; |
Raymond Hettinger | 9bda1d6 | 2005-09-16 07:14:21 +0000 | [diff] [blame] | 148 | * This means we can always use _PyString_Eq directly and not have to check to |
| 149 | * see if the comparison altered the table. |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 150 | */ |
| 151 | static setentry * |
| 152 | set_lookkey_string(PySetObject *so, PyObject *key, register long hash) |
| 153 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 154 | register Py_ssize_t i; |
| 155 | register size_t perturb; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 156 | register setentry *freeslot; |
Neal Norwitz | 0f2783c | 2006-06-19 05:40:44 +0000 | [diff] [blame] | 157 | register size_t mask = so->mask; |
Raymond Hettinger | a580c47 | 2005-08-05 17:19:54 +0000 | [diff] [blame] | 158 | setentry *table = so->table; |
Raymond Hettinger | 06d8cf8 | 2005-07-31 15:36:06 +0000 | [diff] [blame] | 159 | register setentry *entry; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 160 | |
| 161 | /* Make sure this function doesn't have to handle non-string keys, |
| 162 | including subclasses of str; e.g., one reason to subclass |
| 163 | strings is to override __eq__, and for speed we don't cater to |
| 164 | that here. */ |
| 165 | if (!PyString_CheckExact(key)) { |
| 166 | so->lookup = set_lookkey; |
| 167 | return set_lookkey(so, key, hash); |
| 168 | } |
| 169 | i = hash & mask; |
Raymond Hettinger | a580c47 | 2005-08-05 17:19:54 +0000 | [diff] [blame] | 170 | entry = &table[i]; |
Raymond Hettinger | 06d8cf8 | 2005-07-31 15:36:06 +0000 | [diff] [blame] | 171 | if (entry->key == NULL || entry->key == key) |
| 172 | return entry; |
Raymond Hettinger | ed6c1ef | 2005-08-13 08:28:03 +0000 | [diff] [blame] | 173 | if (entry->key == dummy) |
| 174 | freeslot = entry; |
| 175 | else { |
Raymond Hettinger | 06d8cf8 | 2005-07-31 15:36:06 +0000 | [diff] [blame] | 176 | if (entry->hash == hash && _PyString_Eq(entry->key, key)) |
| 177 | return entry; |
Raymond Hettinger | ed6c1ef | 2005-08-13 08:28:03 +0000 | [diff] [blame] | 178 | freeslot = NULL; |
| 179 | } |
| 180 | |
| 181 | /* In the loop, key == dummy is by far (factor of 100s) the |
| 182 | least likely outcome, so test for that last. */ |
| 183 | for (perturb = hash; ; perturb >>= PERTURB_SHIFT) { |
| 184 | i = (i << 2) + i + perturb + 1; |
| 185 | entry = &table[i & mask]; |
| 186 | if (entry->key == NULL) |
| 187 | return freeslot == NULL ? entry : freeslot; |
| 188 | if (entry->key == key |
| 189 | || (entry->hash == hash |
| 190 | && entry->key != dummy |
| 191 | && _PyString_Eq(entry->key, key))) |
| 192 | return entry; |
| 193 | if (entry->key == dummy && freeslot == NULL) |
| 194 | freeslot = entry; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 195 | } |
Neal Norwitz | a5ccda9 | 2006-10-28 21:16:54 +0000 | [diff] [blame] | 196 | assert(0); /* NOT REACHED */ |
| 197 | return 0; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | /* |
Raymond Hettinger | 06d8cf8 | 2005-07-31 15:36:06 +0000 | [diff] [blame] | 201 | Internal routine to insert a new key into the table. |
Raymond Hettinger | 0c85086 | 2006-12-08 04:24:33 +0000 | [diff] [blame] | 202 | Used by the public insert routine. |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 203 | Eats a reference to key. |
| 204 | */ |
Raymond Hettinger | 9bda1d6 | 2005-09-16 07:14:21 +0000 | [diff] [blame] | 205 | static int |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 206 | set_insert_key(register PySetObject *so, PyObject *key, long hash) |
| 207 | { |
Raymond Hettinger | 06d8cf8 | 2005-07-31 15:36:06 +0000 | [diff] [blame] | 208 | register setentry *entry; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 209 | typedef setentry *(*lookupfunc)(PySetObject *, PyObject *, long); |
| 210 | |
| 211 | assert(so->lookup != NULL); |
Raymond Hettinger | 06d8cf8 | 2005-07-31 15:36:06 +0000 | [diff] [blame] | 212 | entry = so->lookup(so, key, hash); |
Raymond Hettinger | 9bda1d6 | 2005-09-16 07:14:21 +0000 | [diff] [blame] | 213 | if (entry == NULL) |
| 214 | return -1; |
Raymond Hettinger | 06d8cf8 | 2005-07-31 15:36:06 +0000 | [diff] [blame] | 215 | if (entry->key == NULL) { |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 216 | /* UNUSED */ |
| 217 | so->fill++; |
Raymond Hettinger | 06d8cf8 | 2005-07-31 15:36:06 +0000 | [diff] [blame] | 218 | entry->key = key; |
| 219 | entry->hash = hash; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 220 | so->used++; |
Raymond Hettinger | 06d8cf8 | 2005-07-31 15:36:06 +0000 | [diff] [blame] | 221 | } else if (entry->key == dummy) { |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 222 | /* DUMMY */ |
Raymond Hettinger | 06d8cf8 | 2005-07-31 15:36:06 +0000 | [diff] [blame] | 223 | entry->key = key; |
| 224 | entry->hash = hash; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 225 | so->used++; |
| 226 | Py_DECREF(dummy); |
| 227 | } else { |
| 228 | /* ACTIVE */ |
| 229 | Py_DECREF(key); |
| 230 | } |
Raymond Hettinger | 9bda1d6 | 2005-09-16 07:14:21 +0000 | [diff] [blame] | 231 | return 0; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | /* |
Raymond Hettinger | 0c85086 | 2006-12-08 04:24:33 +0000 | [diff] [blame] | 235 | Internal routine used by set_table_resize() to insert an item which is |
| 236 | known to be absent from the set. This routine also assumes that |
| 237 | the set contains no deleted entries. Besides the performance benefit, |
| 238 | using set_insert_clean() in set_table_resize() is dangerous (SF bug #1456209). |
| 239 | Note that no refcounts are changed by this routine; if needed, the caller |
| 240 | is responsible for incref'ing `key`. |
| 241 | */ |
| 242 | static void |
| 243 | set_insert_clean(register PySetObject *so, PyObject *key, long hash) |
| 244 | { |
| 245 | register size_t i; |
| 246 | register size_t perturb; |
| 247 | register size_t mask = (size_t)so->mask; |
| 248 | setentry *table = so->table; |
| 249 | register setentry *entry; |
| 250 | |
| 251 | i = hash & mask; |
| 252 | entry = &table[i]; |
| 253 | for (perturb = hash; entry->key != NULL; perturb >>= PERTURB_SHIFT) { |
| 254 | i = (i << 2) + i + perturb + 1; |
| 255 | entry = &table[i & mask]; |
| 256 | } |
| 257 | so->fill++; |
| 258 | entry->key = key; |
| 259 | entry->hash = hash; |
| 260 | so->used++; |
| 261 | } |
| 262 | |
| 263 | /* |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 264 | Restructure the table by allocating a new table and reinserting all |
Raymond Hettinger | 06d8cf8 | 2005-07-31 15:36:06 +0000 | [diff] [blame] | 265 | keys again. When entries have been deleted, the new table may |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 266 | actually be smaller than the old one. |
| 267 | */ |
| 268 | static int |
Neal Norwitz | 0f2783c | 2006-06-19 05:40:44 +0000 | [diff] [blame] | 269 | set_table_resize(PySetObject *so, Py_ssize_t minused) |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 270 | { |
Neal Norwitz | 0f2783c | 2006-06-19 05:40:44 +0000 | [diff] [blame] | 271 | Py_ssize_t newsize; |
Raymond Hettinger | 06d8cf8 | 2005-07-31 15:36:06 +0000 | [diff] [blame] | 272 | setentry *oldtable, *newtable, *entry; |
Neal Norwitz | 0f2783c | 2006-06-19 05:40:44 +0000 | [diff] [blame] | 273 | Py_ssize_t i; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 274 | int is_oldtable_malloced; |
| 275 | setentry small_copy[PySet_MINSIZE]; |
| 276 | |
| 277 | assert(minused >= 0); |
| 278 | |
| 279 | /* Find the smallest table size > minused. */ |
| 280 | for (newsize = PySet_MINSIZE; |
| 281 | newsize <= minused && newsize > 0; |
| 282 | newsize <<= 1) |
| 283 | ; |
| 284 | if (newsize <= 0) { |
| 285 | PyErr_NoMemory(); |
| 286 | return -1; |
| 287 | } |
| 288 | |
| 289 | /* Get space for a new table. */ |
| 290 | oldtable = so->table; |
| 291 | assert(oldtable != NULL); |
| 292 | is_oldtable_malloced = oldtable != so->smalltable; |
| 293 | |
| 294 | if (newsize == PySet_MINSIZE) { |
| 295 | /* A large table is shrinking, or we can't get any smaller. */ |
| 296 | newtable = so->smalltable; |
| 297 | if (newtable == oldtable) { |
| 298 | if (so->fill == so->used) { |
| 299 | /* No dummies, so no point doing anything. */ |
| 300 | return 0; |
| 301 | } |
| 302 | /* We're not going to resize it, but rebuild the |
| 303 | table anyway to purge old dummy entries. |
| 304 | Subtle: This is *necessary* if fill==size, |
| 305 | as set_lookkey needs at least one virgin slot to |
| 306 | terminate failing searches. If fill < size, it's |
| 307 | merely desirable, as dummies slow searches. */ |
| 308 | assert(so->fill > so->used); |
| 309 | memcpy(small_copy, oldtable, sizeof(small_copy)); |
| 310 | oldtable = small_copy; |
| 311 | } |
| 312 | } |
| 313 | else { |
| 314 | newtable = PyMem_NEW(setentry, newsize); |
| 315 | if (newtable == NULL) { |
| 316 | PyErr_NoMemory(); |
| 317 | return -1; |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | /* Make the set empty, using the new table. */ |
| 322 | assert(newtable != oldtable); |
| 323 | so->table = newtable; |
| 324 | so->mask = newsize - 1; |
| 325 | memset(newtable, 0, sizeof(setentry) * newsize); |
| 326 | so->used = 0; |
| 327 | i = so->fill; |
| 328 | so->fill = 0; |
| 329 | |
| 330 | /* Copy the data over; this is refcount-neutral for active entries; |
| 331 | dummy entries aren't copied over, of course */ |
Raymond Hettinger | 06d8cf8 | 2005-07-31 15:36:06 +0000 | [diff] [blame] | 332 | for (entry = oldtable; i > 0; entry++) { |
| 333 | if (entry->key == NULL) { |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 334 | /* UNUSED */ |
| 335 | ; |
Raymond Hettinger | 06d8cf8 | 2005-07-31 15:36:06 +0000 | [diff] [blame] | 336 | } else if (entry->key == dummy) { |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 337 | /* DUMMY */ |
| 338 | --i; |
Raymond Hettinger | 06d8cf8 | 2005-07-31 15:36:06 +0000 | [diff] [blame] | 339 | assert(entry->key == dummy); |
| 340 | Py_DECREF(entry->key); |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 341 | } else { |
| 342 | /* ACTIVE */ |
| 343 | --i; |
Raymond Hettinger | 0c85086 | 2006-12-08 04:24:33 +0000 | [diff] [blame] | 344 | set_insert_clean(so, entry->key, entry->hash); |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 345 | } |
| 346 | } |
| 347 | |
| 348 | if (is_oldtable_malloced) |
| 349 | PyMem_DEL(oldtable); |
| 350 | return 0; |
| 351 | } |
| 352 | |
Raymond Hettinger | c991db2 | 2005-08-11 07:58:45 +0000 | [diff] [blame] | 353 | /* CAUTION: set_add_key/entry() must guarantee it won't resize the table */ |
| 354 | |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 355 | static int |
Raymond Hettinger | c991db2 | 2005-08-11 07:58:45 +0000 | [diff] [blame] | 356 | set_add_entry(register PySetObject *so, setentry *entry) |
| 357 | { |
Neal Norwitz | 0f2783c | 2006-06-19 05:40:44 +0000 | [diff] [blame] | 358 | register Py_ssize_t n_used; |
Raymond Hettinger | c991db2 | 2005-08-11 07:58:45 +0000 | [diff] [blame] | 359 | |
| 360 | assert(so->fill <= so->mask); /* at least one empty slot */ |
| 361 | n_used = so->used; |
| 362 | Py_INCREF(entry->key); |
Raymond Hettinger | c563a1c | 2006-09-07 02:42:48 +0000 | [diff] [blame] | 363 | if (set_insert_key(so, entry->key, entry->hash) == -1) { |
| 364 | Py_DECREF(entry->key); |
Raymond Hettinger | 9bda1d6 | 2005-09-16 07:14:21 +0000 | [diff] [blame] | 365 | return -1; |
Raymond Hettinger | c563a1c | 2006-09-07 02:42:48 +0000 | [diff] [blame] | 366 | } |
Raymond Hettinger | c991db2 | 2005-08-11 07:58:45 +0000 | [diff] [blame] | 367 | if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2)) |
| 368 | return 0; |
| 369 | return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4); |
| 370 | } |
| 371 | |
| 372 | static int |
| 373 | set_add_key(register PySetObject *so, PyObject *key) |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 374 | { |
| 375 | register long hash; |
Neal Norwitz | 0f2783c | 2006-06-19 05:40:44 +0000 | [diff] [blame] | 376 | register Py_ssize_t n_used; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 377 | |
Raymond Hettinger | c991db2 | 2005-08-11 07:58:45 +0000 | [diff] [blame] | 378 | if (!PyString_CheckExact(key) || |
| 379 | (hash = ((PyStringObject *) key)->ob_shash) == -1) { |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 380 | hash = PyObject_Hash(key); |
| 381 | if (hash == -1) |
| 382 | return -1; |
| 383 | } |
| 384 | assert(so->fill <= so->mask); /* at least one empty slot */ |
| 385 | n_used = so->used; |
| 386 | Py_INCREF(key); |
Raymond Hettinger | 9bda1d6 | 2005-09-16 07:14:21 +0000 | [diff] [blame] | 387 | if (set_insert_key(so, key, hash) == -1) { |
| 388 | Py_DECREF(key); |
| 389 | return -1; |
| 390 | } |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 391 | if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2)) |
| 392 | return 0; |
Raymond Hettinger | bc841a1 | 2005-08-07 13:02:53 +0000 | [diff] [blame] | 393 | return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4); |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | #define DISCARD_NOTFOUND 0 |
| 397 | #define DISCARD_FOUND 1 |
| 398 | |
| 399 | static int |
Raymond Hettinger | c991db2 | 2005-08-11 07:58:45 +0000 | [diff] [blame] | 400 | set_discard_entry(PySetObject *so, setentry *oldentry) |
| 401 | { register setentry *entry; |
| 402 | PyObject *old_key; |
| 403 | |
| 404 | entry = (so->lookup)(so, oldentry->key, oldentry->hash); |
Raymond Hettinger | 9bda1d6 | 2005-09-16 07:14:21 +0000 | [diff] [blame] | 405 | if (entry == NULL) |
| 406 | return -1; |
Raymond Hettinger | c991db2 | 2005-08-11 07:58:45 +0000 | [diff] [blame] | 407 | if (entry->key == NULL || entry->key == dummy) |
| 408 | return DISCARD_NOTFOUND; |
| 409 | old_key = entry->key; |
| 410 | Py_INCREF(dummy); |
| 411 | entry->key = dummy; |
| 412 | so->used--; |
| 413 | Py_DECREF(old_key); |
| 414 | return DISCARD_FOUND; |
| 415 | } |
| 416 | |
| 417 | static int |
| 418 | set_discard_key(PySetObject *so, PyObject *key) |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 419 | { |
| 420 | register long hash; |
Raymond Hettinger | 06d8cf8 | 2005-07-31 15:36:06 +0000 | [diff] [blame] | 421 | register setentry *entry; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 422 | PyObject *old_key; |
| 423 | |
| 424 | assert (PyAnySet_Check(so)); |
| 425 | if (!PyString_CheckExact(key) || |
| 426 | (hash = ((PyStringObject *) key)->ob_shash) == -1) { |
| 427 | hash = PyObject_Hash(key); |
| 428 | if (hash == -1) |
| 429 | return -1; |
| 430 | } |
Raymond Hettinger | 06d8cf8 | 2005-07-31 15:36:06 +0000 | [diff] [blame] | 431 | entry = (so->lookup)(so, key, hash); |
Raymond Hettinger | 9bda1d6 | 2005-09-16 07:14:21 +0000 | [diff] [blame] | 432 | if (entry == NULL) |
| 433 | return -1; |
Raymond Hettinger | 06d8cf8 | 2005-07-31 15:36:06 +0000 | [diff] [blame] | 434 | if (entry->key == NULL || entry->key == dummy) |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 435 | return DISCARD_NOTFOUND; |
Raymond Hettinger | 06d8cf8 | 2005-07-31 15:36:06 +0000 | [diff] [blame] | 436 | old_key = entry->key; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 437 | Py_INCREF(dummy); |
Raymond Hettinger | 06d8cf8 | 2005-07-31 15:36:06 +0000 | [diff] [blame] | 438 | entry->key = dummy; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 439 | so->used--; |
| 440 | Py_DECREF(old_key); |
| 441 | return DISCARD_FOUND; |
| 442 | } |
| 443 | |
Raymond Hettinger | fe889f3 | 2005-08-06 05:43:39 +0000 | [diff] [blame] | 444 | static int |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 445 | set_clear_internal(PySetObject *so) |
| 446 | { |
Raymond Hettinger | 06d8cf8 | 2005-07-31 15:36:06 +0000 | [diff] [blame] | 447 | setentry *entry, *table; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 448 | int table_is_malloced; |
Neal Norwitz | 0f2783c | 2006-06-19 05:40:44 +0000 | [diff] [blame] | 449 | Py_ssize_t fill; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 450 | setentry small_copy[PySet_MINSIZE]; |
| 451 | #ifdef Py_DEBUG |
Neal Norwitz | 0f2783c | 2006-06-19 05:40:44 +0000 | [diff] [blame] | 452 | Py_ssize_t i, n; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 453 | assert (PyAnySet_Check(so)); |
Raymond Hettinger | a580c47 | 2005-08-05 17:19:54 +0000 | [diff] [blame] | 454 | |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 455 | n = so->mask + 1; |
| 456 | i = 0; |
| 457 | #endif |
| 458 | |
| 459 | table = so->table; |
| 460 | assert(table != NULL); |
| 461 | table_is_malloced = table != so->smalltable; |
| 462 | |
| 463 | /* This is delicate. During the process of clearing the set, |
| 464 | * decrefs can cause the set to mutate. To avoid fatal confusion |
| 465 | * (voice of experience), we have to make the set empty before |
Raymond Hettinger | fe889f3 | 2005-08-06 05:43:39 +0000 | [diff] [blame] | 466 | * clearing the slots, and never refer to anything via so->ref while |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 467 | * clearing. |
| 468 | */ |
| 469 | fill = so->fill; |
| 470 | if (table_is_malloced) |
| 471 | EMPTY_TO_MINSIZE(so); |
| 472 | |
| 473 | else if (fill > 0) { |
| 474 | /* It's a small table with something that needs to be cleared. |
| 475 | * Afraid the only safe way is to copy the set entries into |
| 476 | * another small table first. |
| 477 | */ |
| 478 | memcpy(small_copy, table, sizeof(small_copy)); |
| 479 | table = small_copy; |
| 480 | EMPTY_TO_MINSIZE(so); |
| 481 | } |
| 482 | /* else it's a small table that's already empty */ |
| 483 | |
| 484 | /* Now we can finally clear things. If C had refcounts, we could |
| 485 | * assert that the refcount on table is 1 now, i.e. that this function |
| 486 | * has unique access to it, so decref side-effects can't alter it. |
| 487 | */ |
Raymond Hettinger | 06d8cf8 | 2005-07-31 15:36:06 +0000 | [diff] [blame] | 488 | for (entry = table; fill > 0; ++entry) { |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 489 | #ifdef Py_DEBUG |
| 490 | assert(i < n); |
| 491 | ++i; |
| 492 | #endif |
Raymond Hettinger | 06d8cf8 | 2005-07-31 15:36:06 +0000 | [diff] [blame] | 493 | if (entry->key) { |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 494 | --fill; |
Raymond Hettinger | 06d8cf8 | 2005-07-31 15:36:06 +0000 | [diff] [blame] | 495 | Py_DECREF(entry->key); |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 496 | } |
| 497 | #ifdef Py_DEBUG |
| 498 | else |
Raymond Hettinger | 334b5b2 | 2006-03-26 03:11:29 +0000 | [diff] [blame] | 499 | assert(entry->key == NULL); |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 500 | #endif |
| 501 | } |
| 502 | |
| 503 | if (table_is_malloced) |
| 504 | PyMem_DEL(table); |
Raymond Hettinger | fe889f3 | 2005-08-06 05:43:39 +0000 | [diff] [blame] | 505 | return 0; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | /* |
| 509 | * Iterate over a set table. Use like so: |
| 510 | * |
Neal Norwitz | 0f2783c | 2006-06-19 05:40:44 +0000 | [diff] [blame] | 511 | * Py_ssize_t pos; |
Raymond Hettinger | c991db2 | 2005-08-11 07:58:45 +0000 | [diff] [blame] | 512 | * setentry *entry; |
Raymond Hettinger | d794666 | 2005-08-01 21:39:29 +0000 | [diff] [blame] | 513 | * pos = 0; # important! pos should not otherwise be changed by you |
Raymond Hettinger | c991db2 | 2005-08-11 07:58:45 +0000 | [diff] [blame] | 514 | * while (set_next(yourset, &pos, &entry)) { |
| 515 | * Refer to borrowed reference in entry->key. |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 516 | * } |
| 517 | * |
Raymond Hettinger | c991db2 | 2005-08-11 07:58:45 +0000 | [diff] [blame] | 518 | * CAUTION: In general, it isn't safe to use set_next in a loop that |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 519 | * mutates the table. |
| 520 | */ |
| 521 | static int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 522 | set_next(PySetObject *so, Py_ssize_t *pos_ptr, setentry **entry_ptr) |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 523 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 524 | Py_ssize_t i; |
Neal Norwitz | 0f2783c | 2006-06-19 05:40:44 +0000 | [diff] [blame] | 525 | Py_ssize_t mask; |
Raymond Hettinger | c991db2 | 2005-08-11 07:58:45 +0000 | [diff] [blame] | 526 | register setentry *table; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 527 | |
| 528 | assert (PyAnySet_Check(so)); |
Raymond Hettinger | c991db2 | 2005-08-11 07:58:45 +0000 | [diff] [blame] | 529 | i = *pos_ptr; |
Raymond Hettinger | f408ddf | 2005-08-17 00:27:42 +0000 | [diff] [blame] | 530 | assert(i >= 0); |
Raymond Hettinger | c991db2 | 2005-08-11 07:58:45 +0000 | [diff] [blame] | 531 | table = so->table; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 532 | mask = so->mask; |
Raymond Hettinger | c991db2 | 2005-08-11 07:58:45 +0000 | [diff] [blame] | 533 | while (i <= mask && (table[i].key == NULL || table[i].key == dummy)) |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 534 | i++; |
Raymond Hettinger | c991db2 | 2005-08-11 07:58:45 +0000 | [diff] [blame] | 535 | *pos_ptr = i+1; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 536 | if (i > mask) |
| 537 | return 0; |
Raymond Hettinger | f408ddf | 2005-08-17 00:27:42 +0000 | [diff] [blame] | 538 | assert(table[i].key != NULL); |
| 539 | *entry_ptr = &table[i]; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 540 | return 1; |
| 541 | } |
| 542 | |
Raymond Hettinger | f408ddf | 2005-08-17 00:27:42 +0000 | [diff] [blame] | 543 | static void |
| 544 | set_dealloc(PySetObject *so) |
| 545 | { |
| 546 | register setentry *entry; |
Neal Norwitz | 0f2783c | 2006-06-19 05:40:44 +0000 | [diff] [blame] | 547 | Py_ssize_t fill = so->fill; |
Raymond Hettinger | f408ddf | 2005-08-17 00:27:42 +0000 | [diff] [blame] | 548 | PyObject_GC_UnTrack(so); |
| 549 | Py_TRASHCAN_SAFE_BEGIN(so) |
| 550 | if (so->weakreflist != NULL) |
| 551 | PyObject_ClearWeakRefs((PyObject *) so); |
| 552 | |
| 553 | for (entry = so->table; fill > 0; entry++) { |
| 554 | if (entry->key) { |
| 555 | --fill; |
| 556 | Py_DECREF(entry->key); |
| 557 | } |
| 558 | } |
| 559 | if (so->table != so->smalltable) |
| 560 | PyMem_DEL(so->table); |
| 561 | if (num_free_sets < MAXFREESETS && PyAnySet_CheckExact(so)) |
| 562 | free_sets[num_free_sets++] = so; |
| 563 | else |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 564 | Py_TYPE(so)->tp_free(so); |
Raymond Hettinger | f408ddf | 2005-08-17 00:27:42 +0000 | [diff] [blame] | 565 | Py_TRASHCAN_SAFE_END(so) |
| 566 | } |
| 567 | |
| 568 | static int |
| 569 | set_tp_print(PySetObject *so, FILE *fp, int flags) |
| 570 | { |
| 571 | setentry *entry; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 572 | Py_ssize_t pos=0; |
Raymond Hettinger | f408ddf | 2005-08-17 00:27:42 +0000 | [diff] [blame] | 573 | char *emit = ""; /* No separator emitted on first pass */ |
| 574 | char *separator = ", "; |
Raymond Hettinger | 5399910 | 2006-12-30 04:01:17 +0000 | [diff] [blame] | 575 | int status = Py_ReprEnter((PyObject*)so); |
| 576 | |
| 577 | if (status != 0) { |
| 578 | if (status < 0) |
| 579 | return status; |
Brett Cannon | 0153159 | 2007-09-17 03:28:34 +0000 | [diff] [blame] | 580 | Py_BEGIN_ALLOW_THREADS |
Raymond Hettinger | 5399910 | 2006-12-30 04:01:17 +0000 | [diff] [blame] | 581 | fprintf(fp, "%s(...)", so->ob_type->tp_name); |
Brett Cannon | 0153159 | 2007-09-17 03:28:34 +0000 | [diff] [blame] | 582 | Py_END_ALLOW_THREADS |
Raymond Hettinger | 5399910 | 2006-12-30 04:01:17 +0000 | [diff] [blame] | 583 | return 0; |
| 584 | } |
Raymond Hettinger | f408ddf | 2005-08-17 00:27:42 +0000 | [diff] [blame] | 585 | |
Brett Cannon | 0153159 | 2007-09-17 03:28:34 +0000 | [diff] [blame] | 586 | Py_BEGIN_ALLOW_THREADS |
Raymond Hettinger | f408ddf | 2005-08-17 00:27:42 +0000 | [diff] [blame] | 587 | fprintf(fp, "%s([", so->ob_type->tp_name); |
Brett Cannon | 0153159 | 2007-09-17 03:28:34 +0000 | [diff] [blame] | 588 | Py_END_ALLOW_THREADS |
Raymond Hettinger | f408ddf | 2005-08-17 00:27:42 +0000 | [diff] [blame] | 589 | while (set_next(so, &pos, &entry)) { |
Brett Cannon | 0153159 | 2007-09-17 03:28:34 +0000 | [diff] [blame] | 590 | Py_BEGIN_ALLOW_THREADS |
Raymond Hettinger | f408ddf | 2005-08-17 00:27:42 +0000 | [diff] [blame] | 591 | fputs(emit, fp); |
Brett Cannon | 0153159 | 2007-09-17 03:28:34 +0000 | [diff] [blame] | 592 | Py_END_ALLOW_THREADS |
Raymond Hettinger | f408ddf | 2005-08-17 00:27:42 +0000 | [diff] [blame] | 593 | emit = separator; |
Raymond Hettinger | 5399910 | 2006-12-30 04:01:17 +0000 | [diff] [blame] | 594 | if (PyObject_Print(entry->key, fp, 0) != 0) { |
| 595 | Py_ReprLeave((PyObject*)so); |
Raymond Hettinger | f408ddf | 2005-08-17 00:27:42 +0000 | [diff] [blame] | 596 | return -1; |
Raymond Hettinger | 5399910 | 2006-12-30 04:01:17 +0000 | [diff] [blame] | 597 | } |
Raymond Hettinger | f408ddf | 2005-08-17 00:27:42 +0000 | [diff] [blame] | 598 | } |
Brett Cannon | 0153159 | 2007-09-17 03:28:34 +0000 | [diff] [blame] | 599 | Py_BEGIN_ALLOW_THREADS |
Raymond Hettinger | f408ddf | 2005-08-17 00:27:42 +0000 | [diff] [blame] | 600 | fputs("])", fp); |
Brett Cannon | 0153159 | 2007-09-17 03:28:34 +0000 | [diff] [blame] | 601 | Py_END_ALLOW_THREADS |
Raymond Hettinger | 5399910 | 2006-12-30 04:01:17 +0000 | [diff] [blame] | 602 | Py_ReprLeave((PyObject*)so); |
Raymond Hettinger | f408ddf | 2005-08-17 00:27:42 +0000 | [diff] [blame] | 603 | return 0; |
| 604 | } |
| 605 | |
| 606 | static PyObject * |
| 607 | set_repr(PySetObject *so) |
| 608 | { |
Raymond Hettinger | 5399910 | 2006-12-30 04:01:17 +0000 | [diff] [blame] | 609 | PyObject *keys, *result=NULL, *listrepr; |
| 610 | int status = Py_ReprEnter((PyObject*)so); |
| 611 | |
| 612 | if (status != 0) { |
| 613 | if (status < 0) |
| 614 | return NULL; |
| 615 | return PyString_FromFormat("%s(...)", so->ob_type->tp_name); |
| 616 | } |
Raymond Hettinger | f408ddf | 2005-08-17 00:27:42 +0000 | [diff] [blame] | 617 | |
| 618 | keys = PySequence_List((PyObject *)so); |
| 619 | if (keys == NULL) |
Raymond Hettinger | 5399910 | 2006-12-30 04:01:17 +0000 | [diff] [blame] | 620 | goto done; |
Raymond Hettinger | f408ddf | 2005-08-17 00:27:42 +0000 | [diff] [blame] | 621 | listrepr = PyObject_Repr(keys); |
| 622 | Py_DECREF(keys); |
| 623 | if (listrepr == NULL) |
Raymond Hettinger | 5399910 | 2006-12-30 04:01:17 +0000 | [diff] [blame] | 624 | goto done; |
Raymond Hettinger | f408ddf | 2005-08-17 00:27:42 +0000 | [diff] [blame] | 625 | |
| 626 | result = PyString_FromFormat("%s(%s)", so->ob_type->tp_name, |
| 627 | PyString_AS_STRING(listrepr)); |
| 628 | Py_DECREF(listrepr); |
Raymond Hettinger | 5399910 | 2006-12-30 04:01:17 +0000 | [diff] [blame] | 629 | done: |
| 630 | Py_ReprLeave((PyObject*)so); |
Raymond Hettinger | f408ddf | 2005-08-17 00:27:42 +0000 | [diff] [blame] | 631 | return result; |
| 632 | } |
| 633 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 634 | static Py_ssize_t |
Raymond Hettinger | f408ddf | 2005-08-17 00:27:42 +0000 | [diff] [blame] | 635 | set_len(PyObject *so) |
| 636 | { |
| 637 | return ((PySetObject *)so)->used; |
| 638 | } |
| 639 | |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 640 | static int |
Raymond Hettinger | c991db2 | 2005-08-11 07:58:45 +0000 | [diff] [blame] | 641 | set_merge(PySetObject *so, PyObject *otherset) |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 642 | { |
Raymond Hettinger | d794666 | 2005-08-01 21:39:29 +0000 | [diff] [blame] | 643 | PySetObject *other; |
Neal Norwitz | 0f2783c | 2006-06-19 05:40:44 +0000 | [diff] [blame] | 644 | register Py_ssize_t i; |
Raymond Hettinger | f408ddf | 2005-08-17 00:27:42 +0000 | [diff] [blame] | 645 | register setentry *entry; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 646 | |
| 647 | assert (PyAnySet_Check(so)); |
Raymond Hettinger | 06d8cf8 | 2005-07-31 15:36:06 +0000 | [diff] [blame] | 648 | assert (PyAnySet_Check(otherset)); |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 649 | |
Raymond Hettinger | 06d8cf8 | 2005-07-31 15:36:06 +0000 | [diff] [blame] | 650 | other = (PySetObject*)otherset; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 651 | if (other == so || other->used == 0) |
| 652 | /* a.update(a) or a.update({}); nothing to do */ |
| 653 | return 0; |
| 654 | /* Do one big resize at the start, rather than |
Raymond Hettinger | 06d8cf8 | 2005-07-31 15:36:06 +0000 | [diff] [blame] | 655 | * incrementally resizing as we insert new keys. Expect |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 656 | * that there will be no (or few) overlapping keys. |
| 657 | */ |
| 658 | if ((so->fill + other->used)*3 >= (so->mask+1)*2) { |
| 659 | if (set_table_resize(so, (so->used + other->used)*2) != 0) |
| 660 | return -1; |
| 661 | } |
Raymond Hettinger | f408ddf | 2005-08-17 00:27:42 +0000 | [diff] [blame] | 662 | for (i = 0; i <= other->mask; i++) { |
| 663 | entry = &other->table[i]; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 664 | if (entry->key != NULL && |
| 665 | entry->key != dummy) { |
| 666 | Py_INCREF(entry->key); |
Raymond Hettinger | 9bda1d6 | 2005-09-16 07:14:21 +0000 | [diff] [blame] | 667 | if (set_insert_key(so, entry->key, entry->hash) == -1) { |
| 668 | Py_DECREF(entry->key); |
| 669 | return -1; |
| 670 | } |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 671 | } |
| 672 | } |
| 673 | return 0; |
| 674 | } |
| 675 | |
| 676 | static int |
Raymond Hettinger | c991db2 | 2005-08-11 07:58:45 +0000 | [diff] [blame] | 677 | set_contains_key(PySetObject *so, PyObject *key) |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 678 | { |
| 679 | long hash; |
Raymond Hettinger | 9bda1d6 | 2005-09-16 07:14:21 +0000 | [diff] [blame] | 680 | setentry *entry; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 681 | |
| 682 | if (!PyString_CheckExact(key) || |
| 683 | (hash = ((PyStringObject *) key)->ob_shash) == -1) { |
| 684 | hash = PyObject_Hash(key); |
| 685 | if (hash == -1) |
| 686 | return -1; |
| 687 | } |
Raymond Hettinger | 9bda1d6 | 2005-09-16 07:14:21 +0000 | [diff] [blame] | 688 | entry = (so->lookup)(so, key, hash); |
| 689 | if (entry == NULL) |
| 690 | return -1; |
| 691 | key = entry->key; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 692 | return key != NULL && key != dummy; |
| 693 | } |
| 694 | |
Raymond Hettinger | c991db2 | 2005-08-11 07:58:45 +0000 | [diff] [blame] | 695 | static int |
| 696 | set_contains_entry(PySetObject *so, setentry *entry) |
| 697 | { |
| 698 | PyObject *key; |
Raymond Hettinger | 9bda1d6 | 2005-09-16 07:14:21 +0000 | [diff] [blame] | 699 | setentry *lu_entry; |
Raymond Hettinger | c991db2 | 2005-08-11 07:58:45 +0000 | [diff] [blame] | 700 | |
Raymond Hettinger | 9bda1d6 | 2005-09-16 07:14:21 +0000 | [diff] [blame] | 701 | lu_entry = (so->lookup)(so, entry->key, entry->hash); |
| 702 | if (lu_entry == NULL) |
| 703 | return -1; |
| 704 | key = lu_entry->key; |
Raymond Hettinger | c991db2 | 2005-08-11 07:58:45 +0000 | [diff] [blame] | 705 | return key != NULL && key != dummy; |
| 706 | } |
| 707 | |
Raymond Hettinger | ce8185e | 2005-08-13 09:28:48 +0000 | [diff] [blame] | 708 | static PyObject * |
| 709 | set_pop(PySetObject *so) |
| 710 | { |
Neal Norwitz | 0f2783c | 2006-06-19 05:40:44 +0000 | [diff] [blame] | 711 | register Py_ssize_t i = 0; |
Raymond Hettinger | f408ddf | 2005-08-17 00:27:42 +0000 | [diff] [blame] | 712 | register setentry *entry; |
| 713 | PyObject *key; |
Raymond Hettinger | ce8185e | 2005-08-13 09:28:48 +0000 | [diff] [blame] | 714 | |
| 715 | assert (PyAnySet_Check(so)); |
| 716 | if (so->used == 0) { |
| 717 | PyErr_SetString(PyExc_KeyError, "pop from an empty set"); |
| 718 | return NULL; |
| 719 | } |
| 720 | |
| 721 | /* Set entry to "the first" unused or dummy set entry. We abuse |
| 722 | * the hash field of slot 0 to hold a search finger: |
| 723 | * If slot 0 has a value, use slot 0. |
| 724 | * Else slot 0 is being used to hold a search finger, |
| 725 | * and we use its hash value as the first index to look. |
| 726 | */ |
| 727 | entry = &so->table[0]; |
| 728 | if (entry->key == NULL || entry->key == dummy) { |
Neal Norwitz | 0f2783c | 2006-06-19 05:40:44 +0000 | [diff] [blame] | 729 | i = entry->hash; |
Raymond Hettinger | ce8185e | 2005-08-13 09:28:48 +0000 | [diff] [blame] | 730 | /* The hash field may be a real hash value, or it may be a |
| 731 | * legit search finger, or it may be a once-legit search |
| 732 | * finger that's out of bounds now because it wrapped around |
| 733 | * or the table shrunk -- simply make sure it's in bounds now. |
| 734 | */ |
| 735 | if (i > so->mask || i < 1) |
| 736 | i = 1; /* skip slot 0 */ |
| 737 | while ((entry = &so->table[i])->key == NULL || entry->key==dummy) { |
| 738 | i++; |
| 739 | if (i > so->mask) |
| 740 | i = 1; |
| 741 | } |
| 742 | } |
| 743 | key = entry->key; |
| 744 | Py_INCREF(dummy); |
| 745 | entry->key = dummy; |
| 746 | so->used--; |
| 747 | so->table[0].hash = i + 1; /* next place to start */ |
| 748 | return key; |
| 749 | } |
| 750 | |
| 751 | PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element."); |
| 752 | |
| 753 | static int |
Raymond Hettinger | f408ddf | 2005-08-17 00:27:42 +0000 | [diff] [blame] | 754 | set_traverse(PySetObject *so, visitproc visit, void *arg) |
Raymond Hettinger | ce8185e | 2005-08-13 09:28:48 +0000 | [diff] [blame] | 755 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 756 | Py_ssize_t pos = 0; |
Raymond Hettinger | f408ddf | 2005-08-17 00:27:42 +0000 | [diff] [blame] | 757 | setentry *entry; |
| 758 | |
| 759 | while (set_next(so, &pos, &entry)) |
| 760 | Py_VISIT(entry->key); |
| 761 | return 0; |
| 762 | } |
| 763 | |
| 764 | static long |
| 765 | frozenset_hash(PyObject *self) |
| 766 | { |
| 767 | PySetObject *so = (PySetObject *)self; |
| 768 | long h, hash = 1927868237L; |
| 769 | setentry *entry; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 770 | Py_ssize_t pos = 0; |
Raymond Hettinger | f408ddf | 2005-08-17 00:27:42 +0000 | [diff] [blame] | 771 | |
| 772 | if (so->hash != -1) |
| 773 | return so->hash; |
| 774 | |
| 775 | hash *= PySet_GET_SIZE(self) + 1; |
| 776 | while (set_next(so, &pos, &entry)) { |
| 777 | /* Work to increase the bit dispersion for closely spaced hash |
| 778 | values. The is important because some use cases have many |
| 779 | combinations of a small number of elements with nearby |
| 780 | hashes so that many distinct combinations collapse to only |
| 781 | a handful of distinct hash values. */ |
| 782 | h = entry->hash; |
| 783 | hash ^= (h ^ (h << 16) ^ 89869747L) * 3644798167u; |
| 784 | } |
| 785 | hash = hash * 69069L + 907133923L; |
| 786 | if (hash == -1) |
| 787 | hash = 590923713L; |
| 788 | so->hash = hash; |
| 789 | return hash; |
| 790 | } |
| 791 | |
Raymond Hettinger | a9d9936 | 2005-08-05 00:01:15 +0000 | [diff] [blame] | 792 | /***** Set iterator type ***********************************************/ |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 793 | |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 794 | typedef struct { |
| 795 | PyObject_HEAD |
| 796 | PySetObject *si_set; /* Set to NULL when iterator is exhausted */ |
Neal Norwitz | 0f2783c | 2006-06-19 05:40:44 +0000 | [diff] [blame] | 797 | Py_ssize_t si_used; |
| 798 | Py_ssize_t si_pos; |
| 799 | Py_ssize_t len; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 800 | } setiterobject; |
| 801 | |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 802 | static void |
| 803 | setiter_dealloc(setiterobject *si) |
| 804 | { |
| 805 | Py_XDECREF(si->si_set); |
| 806 | PyObject_Del(si); |
| 807 | } |
| 808 | |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 809 | static PyObject * |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 810 | setiter_len(setiterobject *si) |
| 811 | { |
Neal Norwitz | 0f2783c | 2006-06-19 05:40:44 +0000 | [diff] [blame] | 812 | Py_ssize_t len = 0; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 813 | if (si->si_set != NULL && si->si_used == si->si_set->used) |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 814 | len = si->len; |
| 815 | return PyInt_FromLong(len); |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 816 | } |
| 817 | |
Armin Rigo | f5b3e36 | 2006-02-11 21:32:43 +0000 | [diff] [blame] | 818 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 819 | |
| 820 | static PyMethodDef setiter_methods[] = { |
Armin Rigo | f5b3e36 | 2006-02-11 21:32:43 +0000 | [diff] [blame] | 821 | {"__length_hint__", (PyCFunction)setiter_len, METH_NOARGS, length_hint_doc}, |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 822 | {NULL, NULL} /* sentinel */ |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 823 | }; |
| 824 | |
Raymond Hettinger | 06d8cf8 | 2005-07-31 15:36:06 +0000 | [diff] [blame] | 825 | static PyObject *setiter_iternext(setiterobject *si) |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 826 | { |
| 827 | PyObject *key; |
Neal Norwitz | 0f2783c | 2006-06-19 05:40:44 +0000 | [diff] [blame] | 828 | register Py_ssize_t i, mask; |
Raymond Hettinger | 06d8cf8 | 2005-07-31 15:36:06 +0000 | [diff] [blame] | 829 | register setentry *entry; |
| 830 | PySetObject *so = si->si_set; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 831 | |
Raymond Hettinger | 06d8cf8 | 2005-07-31 15:36:06 +0000 | [diff] [blame] | 832 | if (so == NULL) |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 833 | return NULL; |
Raymond Hettinger | 06d8cf8 | 2005-07-31 15:36:06 +0000 | [diff] [blame] | 834 | assert (PyAnySet_Check(so)); |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 835 | |
Raymond Hettinger | 06d8cf8 | 2005-07-31 15:36:06 +0000 | [diff] [blame] | 836 | if (si->si_used != so->used) { |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 837 | PyErr_SetString(PyExc_RuntimeError, |
| 838 | "Set changed size during iteration"); |
| 839 | si->si_used = -1; /* Make this state sticky */ |
| 840 | return NULL; |
| 841 | } |
| 842 | |
| 843 | i = si->si_pos; |
Raymond Hettinger | f408ddf | 2005-08-17 00:27:42 +0000 | [diff] [blame] | 844 | assert(i>=0); |
Raymond Hettinger | 06d8cf8 | 2005-07-31 15:36:06 +0000 | [diff] [blame] | 845 | entry = so->table; |
| 846 | mask = so->mask; |
| 847 | while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy)) |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 848 | i++; |
| 849 | si->si_pos = i+1; |
| 850 | if (i > mask) |
| 851 | goto fail; |
| 852 | si->len--; |
Raymond Hettinger | 06d8cf8 | 2005-07-31 15:36:06 +0000 | [diff] [blame] | 853 | key = entry[i].key; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 854 | Py_INCREF(key); |
| 855 | return key; |
| 856 | |
| 857 | fail: |
Raymond Hettinger | 06d8cf8 | 2005-07-31 15:36:06 +0000 | [diff] [blame] | 858 | Py_DECREF(so); |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 859 | si->si_set = NULL; |
| 860 | return NULL; |
| 861 | } |
| 862 | |
Hye-Shik Chang | e295676 | 2005-08-01 05:26:41 +0000 | [diff] [blame] | 863 | static PyTypeObject PySetIter_Type = { |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 864 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Raymond Hettinger | 06d8cf8 | 2005-07-31 15:36:06 +0000 | [diff] [blame] | 865 | "setiterator", /* tp_name */ |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 866 | sizeof(setiterobject), /* tp_basicsize */ |
| 867 | 0, /* tp_itemsize */ |
| 868 | /* methods */ |
| 869 | (destructor)setiter_dealloc, /* tp_dealloc */ |
| 870 | 0, /* tp_print */ |
| 871 | 0, /* tp_getattr */ |
| 872 | 0, /* tp_setattr */ |
| 873 | 0, /* tp_compare */ |
| 874 | 0, /* tp_repr */ |
| 875 | 0, /* tp_as_number */ |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 876 | 0, /* tp_as_sequence */ |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 877 | 0, /* tp_as_mapping */ |
| 878 | 0, /* tp_hash */ |
| 879 | 0, /* tp_call */ |
| 880 | 0, /* tp_str */ |
| 881 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 882 | 0, /* tp_setattro */ |
| 883 | 0, /* tp_as_buffer */ |
| 884 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 885 | 0, /* tp_doc */ |
| 886 | 0, /* tp_traverse */ |
| 887 | 0, /* tp_clear */ |
| 888 | 0, /* tp_richcompare */ |
| 889 | 0, /* tp_weaklistoffset */ |
| 890 | PyObject_SelfIter, /* tp_iter */ |
Raymond Hettinger | 06d8cf8 | 2005-07-31 15:36:06 +0000 | [diff] [blame] | 891 | (iternextfunc)setiter_iternext, /* tp_iternext */ |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 892 | setiter_methods, /* tp_methods */ |
| 893 | 0, |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 894 | }; |
| 895 | |
Martin v. Löwis | 72d2067 | 2006-04-11 09:04:12 +0000 | [diff] [blame] | 896 | static PyObject * |
| 897 | set_iter(PySetObject *so) |
| 898 | { |
| 899 | setiterobject *si = PyObject_New(setiterobject, &PySetIter_Type); |
| 900 | if (si == NULL) |
| 901 | return NULL; |
| 902 | Py_INCREF(so); |
| 903 | si->si_set = so; |
| 904 | si->si_used = so->used; |
| 905 | si->si_pos = 0; |
| 906 | si->len = so->used; |
| 907 | return (PyObject *)si; |
| 908 | } |
| 909 | |
Raymond Hettinger | d794666 | 2005-08-01 21:39:29 +0000 | [diff] [blame] | 910 | static int |
Raymond Hettinger | d794666 | 2005-08-01 21:39:29 +0000 | [diff] [blame] | 911 | set_update_internal(PySetObject *so, PyObject *other) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 912 | { |
Raymond Hettinger | 06d8cf8 | 2005-07-31 15:36:06 +0000 | [diff] [blame] | 913 | PyObject *key, *it; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 914 | |
Raymond Hettinger | 0e7a632 | 2007-02-08 00:50:39 +0000 | [diff] [blame] | 915 | if (PyAnySet_CheckExact(other)) |
Raymond Hettinger | c991db2 | 2005-08-11 07:58:45 +0000 | [diff] [blame] | 916 | return set_merge(so, other); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 917 | |
Raymond Hettinger | db67aef | 2007-02-01 21:02:59 +0000 | [diff] [blame] | 918 | if (PyDict_CheckExact(other)) { |
Neal Norwitz | 0c6e2f1 | 2006-01-08 06:13:44 +0000 | [diff] [blame] | 919 | PyObject *value; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 920 | Py_ssize_t pos = 0; |
Raymond Hettinger | d6fc72a | 2007-02-19 02:03:19 +0000 | [diff] [blame] | 921 | long hash; |
Raymond Hettinger | 15cade0 | 2007-02-19 20:44:04 +0000 | [diff] [blame] | 922 | Py_ssize_t dictsize = PyDict_Size(other); |
Raymond Hettinger | d6fc72a | 2007-02-19 02:03:19 +0000 | [diff] [blame] | 923 | |
Raymond Hettinger | 15cade0 | 2007-02-19 20:44:04 +0000 | [diff] [blame] | 924 | /* Do one big resize at the start, rather than |
| 925 | * incrementally resizing as we insert new keys. Expect |
| 926 | * that there will be no (or few) overlapping keys. |
| 927 | */ |
| 928 | if (dictsize == -1) |
| 929 | return -1; |
| 930 | if ((so->fill + dictsize)*3 >= (so->mask+1)*2) { |
| 931 | if (set_table_resize(so, (so->used + dictsize)*2) != 0) |
| 932 | return -1; |
| 933 | } |
Raymond Hettinger | d6fc72a | 2007-02-19 02:03:19 +0000 | [diff] [blame] | 934 | while (_PyDict_Next(other, &pos, &key, &value, &hash)) { |
| 935 | setentry an_entry; |
| 936 | |
| 937 | an_entry.hash = hash; |
| 938 | an_entry.key = key; |
| 939 | if (set_add_entry(so, &an_entry) == -1) |
Raymond Hettinger | d794666 | 2005-08-01 21:39:29 +0000 | [diff] [blame] | 940 | return -1; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 941 | } |
Raymond Hettinger | d794666 | 2005-08-01 21:39:29 +0000 | [diff] [blame] | 942 | return 0; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 943 | } |
| 944 | |
Raymond Hettinger | a38123e | 2003-11-24 22:18:49 +0000 | [diff] [blame] | 945 | it = PyObject_GetIter(other); |
| 946 | if (it == NULL) |
Raymond Hettinger | d794666 | 2005-08-01 21:39:29 +0000 | [diff] [blame] | 947 | return -1; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 948 | |
Raymond Hettinger | 06d8cf8 | 2005-07-31 15:36:06 +0000 | [diff] [blame] | 949 | while ((key = PyIter_Next(it)) != NULL) { |
Raymond Hettinger | c991db2 | 2005-08-11 07:58:45 +0000 | [diff] [blame] | 950 | if (set_add_key(so, key) == -1) { |
Raymond Hettinger | a38123e | 2003-11-24 22:18:49 +0000 | [diff] [blame] | 951 | Py_DECREF(it); |
Raymond Hettinger | 06d8cf8 | 2005-07-31 15:36:06 +0000 | [diff] [blame] | 952 | Py_DECREF(key); |
Raymond Hettinger | d794666 | 2005-08-01 21:39:29 +0000 | [diff] [blame] | 953 | return -1; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 954 | } |
Raymond Hettinger | 06d8cf8 | 2005-07-31 15:36:06 +0000 | [diff] [blame] | 955 | Py_DECREF(key); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 956 | } |
Raymond Hettinger | a38123e | 2003-11-24 22:18:49 +0000 | [diff] [blame] | 957 | Py_DECREF(it); |
Raymond Hettinger | f5f41bf | 2003-11-24 02:57:33 +0000 | [diff] [blame] | 958 | if (PyErr_Occurred()) |
Raymond Hettinger | d794666 | 2005-08-01 21:39:29 +0000 | [diff] [blame] | 959 | return -1; |
| 960 | return 0; |
| 961 | } |
| 962 | |
| 963 | static PyObject * |
| 964 | set_update(PySetObject *so, PyObject *other) |
| 965 | { |
| 966 | if (set_update_internal(so, other) == -1) |
Raymond Hettinger | a38123e | 2003-11-24 22:18:49 +0000 | [diff] [blame] | 967 | return NULL; |
| 968 | Py_RETURN_NONE; |
| 969 | } |
| 970 | |
| 971 | PyDoc_STRVAR(update_doc, |
| 972 | "Update a set with the union of itself and another."); |
| 973 | |
| 974 | static PyObject * |
| 975 | make_new_set(PyTypeObject *type, PyObject *iterable) |
| 976 | { |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 977 | register PySetObject *so = NULL; |
Raymond Hettinger | a38123e | 2003-11-24 22:18:49 +0000 | [diff] [blame] | 978 | |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 979 | if (dummy == NULL) { /* Auto-initialize dummy */ |
| 980 | dummy = PyString_FromString("<dummy key>"); |
| 981 | if (dummy == NULL) |
| 982 | return NULL; |
| 983 | } |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 984 | |
| 985 | /* create PySetObject structure */ |
Raymond Hettinger | bc841a1 | 2005-08-07 13:02:53 +0000 | [diff] [blame] | 986 | if (num_free_sets && |
| 987 | (type == &PySet_Type || type == &PyFrozenSet_Type)) { |
| 988 | so = free_sets[--num_free_sets]; |
| 989 | assert (so != NULL && PyAnySet_CheckExact(so)); |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 990 | Py_TYPE(so) = type; |
Raymond Hettinger | bc841a1 | 2005-08-07 13:02:53 +0000 | [diff] [blame] | 991 | _Py_NewReference((PyObject *)so); |
| 992 | EMPTY_TO_MINSIZE(so); |
| 993 | PyObject_GC_Track(so); |
| 994 | } else { |
| 995 | so = (PySetObject *)type->tp_alloc(type, 0); |
| 996 | if (so == NULL) |
| 997 | return NULL; |
| 998 | /* tp_alloc has already zeroed the structure */ |
| 999 | assert(so->table == NULL && so->fill == 0 && so->used == 0); |
| 1000 | INIT_NONZERO_SET_SLOTS(so); |
| 1001 | } |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1002 | |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1003 | so->lookup = set_lookkey_string; |
Raymond Hettinger | 691d805 | 2004-05-30 07:26:47 +0000 | [diff] [blame] | 1004 | so->weakreflist = NULL; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1005 | |
Raymond Hettinger | a38123e | 2003-11-24 22:18:49 +0000 | [diff] [blame] | 1006 | if (iterable != NULL) { |
Raymond Hettinger | d794666 | 2005-08-01 21:39:29 +0000 | [diff] [blame] | 1007 | if (set_update_internal(so, iterable) == -1) { |
Raymond Hettinger | a38123e | 2003-11-24 22:18:49 +0000 | [diff] [blame] | 1008 | Py_DECREF(so); |
| 1009 | return NULL; |
| 1010 | } |
Raymond Hettinger | a38123e | 2003-11-24 22:18:49 +0000 | [diff] [blame] | 1011 | } |
| 1012 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1013 | return (PyObject *)so; |
| 1014 | } |
| 1015 | |
Raymond Hettinger | d794666 | 2005-08-01 21:39:29 +0000 | [diff] [blame] | 1016 | /* The empty frozenset is a singleton */ |
| 1017 | static PyObject *emptyfrozenset = NULL; |
| 1018 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1019 | static PyObject * |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 1020 | frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1021 | { |
Raymond Hettinger | d794666 | 2005-08-01 21:39:29 +0000 | [diff] [blame] | 1022 | PyObject *iterable = NULL, *result; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1023 | |
Raymond Hettinger | 9fdfadb | 2007-01-11 18:22:55 +0000 | [diff] [blame] | 1024 | if (type == &PyFrozenSet_Type && !_PyArg_NoKeywords("frozenset()", kwds)) |
Georg Brandl | 02c4287 | 2005-08-26 06:42:30 +0000 | [diff] [blame] | 1025 | return NULL; |
| 1026 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1027 | if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable)) |
| 1028 | return NULL; |
Raymond Hettinger | d794666 | 2005-08-01 21:39:29 +0000 | [diff] [blame] | 1029 | |
| 1030 | if (type != &PyFrozenSet_Type) |
| 1031 | return make_new_set(type, iterable); |
| 1032 | |
| 1033 | if (iterable != NULL) { |
| 1034 | /* frozenset(f) is idempotent */ |
| 1035 | if (PyFrozenSet_CheckExact(iterable)) { |
| 1036 | Py_INCREF(iterable); |
| 1037 | return iterable; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1038 | } |
Raymond Hettinger | d794666 | 2005-08-01 21:39:29 +0000 | [diff] [blame] | 1039 | result = make_new_set(type, iterable); |
Raymond Hettinger | beb3101 | 2005-08-16 03:47:52 +0000 | [diff] [blame] | 1040 | if (result == NULL || PySet_GET_SIZE(result)) |
Raymond Hettinger | d794666 | 2005-08-01 21:39:29 +0000 | [diff] [blame] | 1041 | return result; |
| 1042 | Py_DECREF(result); |
Raymond Hettinger | 49ba4c3 | 2003-11-23 02:49:05 +0000 | [diff] [blame] | 1043 | } |
Raymond Hettinger | d794666 | 2005-08-01 21:39:29 +0000 | [diff] [blame] | 1044 | /* The empty frozenset is a singleton */ |
| 1045 | if (emptyfrozenset == NULL) |
| 1046 | emptyfrozenset = make_new_set(type, NULL); |
| 1047 | Py_XINCREF(emptyfrozenset); |
| 1048 | return emptyfrozenset; |
| 1049 | } |
| 1050 | |
| 1051 | void |
| 1052 | PySet_Fini(void) |
| 1053 | { |
Raymond Hettinger | bc841a1 | 2005-08-07 13:02:53 +0000 | [diff] [blame] | 1054 | PySetObject *so; |
| 1055 | |
| 1056 | while (num_free_sets) { |
| 1057 | num_free_sets--; |
| 1058 | so = free_sets[num_free_sets]; |
| 1059 | PyObject_GC_Del(so); |
| 1060 | } |
Martin v. Löwis | ed8f783 | 2006-04-15 12:47:23 +0000 | [diff] [blame] | 1061 | Py_CLEAR(dummy); |
| 1062 | Py_CLEAR(emptyfrozenset); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1063 | } |
| 1064 | |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 1065 | static PyObject * |
| 1066 | set_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 1067 | { |
Raymond Hettinger | 9fdfadb | 2007-01-11 18:22:55 +0000 | [diff] [blame] | 1068 | if (type == &PySet_Type && !_PyArg_NoKeywords("set()", kwds)) |
Georg Brandl | 02c4287 | 2005-08-26 06:42:30 +0000 | [diff] [blame] | 1069 | return NULL; |
| 1070 | |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 1071 | return make_new_set(type, NULL); |
| 1072 | } |
| 1073 | |
Raymond Hettinger | 934d63e | 2005-07-31 01:33:10 +0000 | [diff] [blame] | 1074 | /* set_swap_bodies() switches the contents of any two sets by moving their |
| 1075 | internal data pointers and, if needed, copying the internal smalltables. |
| 1076 | Semantically equivalent to: |
| 1077 | |
| 1078 | t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t |
| 1079 | |
| 1080 | The function always succeeds and it leaves both objects in a stable state. |
| 1081 | Useful for creating temporary frozensets from sets for membership testing |
| 1082 | in __contains__(), discard(), and remove(). Also useful for operations |
| 1083 | that update in-place (by allowing an intermediate result to be swapped |
Raymond Hettinger | 9dcb17c | 2005-07-31 13:09:28 +0000 | [diff] [blame] | 1084 | into one of the original inputs). |
Raymond Hettinger | 934d63e | 2005-07-31 01:33:10 +0000 | [diff] [blame] | 1085 | */ |
| 1086 | |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1087 | static void |
| 1088 | set_swap_bodies(PySetObject *a, PySetObject *b) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1089 | { |
Neal Norwitz | 0f2783c | 2006-06-19 05:40:44 +0000 | [diff] [blame] | 1090 | Py_ssize_t t; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1091 | setentry *u; |
| 1092 | setentry *(*f)(PySetObject *so, PyObject *key, long hash); |
| 1093 | setentry tab[PySet_MINSIZE]; |
| 1094 | long h; |
| 1095 | |
| 1096 | t = a->fill; a->fill = b->fill; b->fill = t; |
| 1097 | t = a->used; a->used = b->used; b->used = t; |
| 1098 | t = a->mask; a->mask = b->mask; b->mask = t; |
| 1099 | |
| 1100 | u = a->table; |
| 1101 | if (a->table == a->smalltable) |
| 1102 | u = b->smalltable; |
| 1103 | a->table = b->table; |
| 1104 | if (b->table == b->smalltable) |
| 1105 | a->table = a->smalltable; |
| 1106 | b->table = u; |
| 1107 | |
| 1108 | f = a->lookup; a->lookup = b->lookup; b->lookup = f; |
| 1109 | |
| 1110 | if (a->table == a->smalltable || b->table == b->smalltable) { |
| 1111 | memcpy(tab, a->smalltable, sizeof(tab)); |
| 1112 | memcpy(a->smalltable, b->smalltable, sizeof(tab)); |
| 1113 | memcpy(b->smalltable, tab, sizeof(tab)); |
| 1114 | } |
| 1115 | |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 1116 | if (PyType_IsSubtype(Py_TYPE(a), &PyFrozenSet_Type) && |
| 1117 | PyType_IsSubtype(Py_TYPE(b), &PyFrozenSet_Type)) { |
Raymond Hettinger | a580c47 | 2005-08-05 17:19:54 +0000 | [diff] [blame] | 1118 | h = a->hash; a->hash = b->hash; b->hash = h; |
| 1119 | } else { |
| 1120 | a->hash = -1; |
| 1121 | b->hash = -1; |
| 1122 | } |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1123 | } |
| 1124 | |
Raymond Hettinger | 8f5cdaa | 2003-12-13 11:26:12 +0000 | [diff] [blame] | 1125 | static PyObject * |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1126 | set_copy(PySetObject *so) |
| 1127 | { |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 1128 | return make_new_set(Py_TYPE(so), (PyObject *)so); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1129 | } |
| 1130 | |
Raymond Hettinger | 49ba4c3 | 2003-11-23 02:49:05 +0000 | [diff] [blame] | 1131 | static PyObject * |
| 1132 | frozenset_copy(PySetObject *so) |
| 1133 | { |
Raymond Hettinger | f5f41bf | 2003-11-24 02:57:33 +0000 | [diff] [blame] | 1134 | if (PyFrozenSet_CheckExact(so)) { |
Raymond Hettinger | 49ba4c3 | 2003-11-23 02:49:05 +0000 | [diff] [blame] | 1135 | Py_INCREF(so); |
| 1136 | return (PyObject *)so; |
| 1137 | } |
| 1138 | return set_copy(so); |
| 1139 | } |
| 1140 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1141 | PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set."); |
| 1142 | |
| 1143 | static PyObject * |
Raymond Hettinger | c991db2 | 2005-08-11 07:58:45 +0000 | [diff] [blame] | 1144 | set_clear(PySetObject *so) |
| 1145 | { |
| 1146 | set_clear_internal(so); |
| 1147 | Py_RETURN_NONE; |
| 1148 | } |
| 1149 | |
| 1150 | PyDoc_STRVAR(clear_doc, "Remove all elements from this set."); |
| 1151 | |
| 1152 | static PyObject * |
Raymond Hettinger | f5f41bf | 2003-11-24 02:57:33 +0000 | [diff] [blame] | 1153 | set_union(PySetObject *so, PyObject *other) |
| 1154 | { |
| 1155 | PySetObject *result; |
Raymond Hettinger | f5f41bf | 2003-11-24 02:57:33 +0000 | [diff] [blame] | 1156 | |
| 1157 | result = (PySetObject *)set_copy(so); |
| 1158 | if (result == NULL) |
| 1159 | return NULL; |
Raymond Hettinger | d8e1338 | 2005-08-17 12:27:17 +0000 | [diff] [blame] | 1160 | if ((PyObject *)so == other) |
| 1161 | return (PyObject *)result; |
Raymond Hettinger | d794666 | 2005-08-01 21:39:29 +0000 | [diff] [blame] | 1162 | if (set_update_internal(result, other) == -1) { |
Raymond Hettinger | f5f41bf | 2003-11-24 02:57:33 +0000 | [diff] [blame] | 1163 | Py_DECREF(result); |
| 1164 | return NULL; |
| 1165 | } |
Raymond Hettinger | f5f41bf | 2003-11-24 02:57:33 +0000 | [diff] [blame] | 1166 | return (PyObject *)result; |
| 1167 | } |
| 1168 | |
| 1169 | PyDoc_STRVAR(union_doc, |
| 1170 | "Return the union of two sets as a new set.\n\ |
| 1171 | \n\ |
| 1172 | (i.e. all elements that are in either set.)"); |
| 1173 | |
| 1174 | static PyObject * |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1175 | set_or(PySetObject *so, PyObject *other) |
| 1176 | { |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 1177 | if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) { |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1178 | Py_INCREF(Py_NotImplemented); |
| 1179 | return Py_NotImplemented; |
| 1180 | } |
| 1181 | return set_union(so, other); |
| 1182 | } |
| 1183 | |
| 1184 | static PyObject * |
| 1185 | set_ior(PySetObject *so, PyObject *other) |
| 1186 | { |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 1187 | if (!PyAnySet_Check(other)) { |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1188 | Py_INCREF(Py_NotImplemented); |
| 1189 | return Py_NotImplemented; |
| 1190 | } |
Raymond Hettinger | d794666 | 2005-08-01 21:39:29 +0000 | [diff] [blame] | 1191 | if (set_update_internal(so, other) == -1) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1192 | return NULL; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1193 | Py_INCREF(so); |
| 1194 | return (PyObject *)so; |
| 1195 | } |
| 1196 | |
| 1197 | static PyObject * |
| 1198 | set_intersection(PySetObject *so, PyObject *other) |
| 1199 | { |
| 1200 | PySetObject *result; |
Raymond Hettinger | 06d8cf8 | 2005-07-31 15:36:06 +0000 | [diff] [blame] | 1201 | PyObject *key, *it, *tmp; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1202 | |
Raymond Hettinger | d8e1338 | 2005-08-17 12:27:17 +0000 | [diff] [blame] | 1203 | if ((PyObject *)so == other) |
| 1204 | return set_copy(so); |
Raymond Hettinger | c991db2 | 2005-08-11 07:58:45 +0000 | [diff] [blame] | 1205 | |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 1206 | result = (PySetObject *)make_new_set(Py_TYPE(so), NULL); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1207 | if (result == NULL) |
| 1208 | return NULL; |
Raymond Hettinger | f5f41bf | 2003-11-24 02:57:33 +0000 | [diff] [blame] | 1209 | |
Raymond Hettinger | 0e7a632 | 2007-02-08 00:50:39 +0000 | [diff] [blame] | 1210 | if (PyAnySet_CheckExact(other)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1211 | Py_ssize_t pos = 0; |
Raymond Hettinger | c991db2 | 2005-08-11 07:58:45 +0000 | [diff] [blame] | 1212 | setentry *entry; |
Raymond Hettinger | b02c35e | 2005-08-12 20:48:39 +0000 | [diff] [blame] | 1213 | |
Raymond Hettinger | beb3101 | 2005-08-16 03:47:52 +0000 | [diff] [blame] | 1214 | if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) { |
Raymond Hettinger | b02c35e | 2005-08-12 20:48:39 +0000 | [diff] [blame] | 1215 | tmp = (PyObject *)so; |
| 1216 | so = (PySetObject *)other; |
| 1217 | other = tmp; |
| 1218 | } |
| 1219 | |
Raymond Hettinger | c991db2 | 2005-08-11 07:58:45 +0000 | [diff] [blame] | 1220 | while (set_next((PySetObject *)other, &pos, &entry)) { |
Raymond Hettinger | c563a1c | 2006-09-07 02:42:48 +0000 | [diff] [blame] | 1221 | int rv = set_contains_entry(so, entry); |
| 1222 | if (rv == -1) { |
| 1223 | Py_DECREF(result); |
| 1224 | return NULL; |
| 1225 | } |
| 1226 | if (rv) { |
Raymond Hettinger | c991db2 | 2005-08-11 07:58:45 +0000 | [diff] [blame] | 1227 | if (set_add_entry(result, entry) == -1) { |
Raymond Hettinger | a3b11e7 | 2003-12-31 14:08:58 +0000 | [diff] [blame] | 1228 | Py_DECREF(result); |
| 1229 | return NULL; |
| 1230 | } |
| 1231 | } |
| 1232 | } |
| 1233 | return (PyObject *)result; |
Raymond Hettinger | f5f41bf | 2003-11-24 02:57:33 +0000 | [diff] [blame] | 1234 | } |
| 1235 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1236 | it = PyObject_GetIter(other); |
| 1237 | if (it == NULL) { |
| 1238 | Py_DECREF(result); |
| 1239 | return NULL; |
| 1240 | } |
| 1241 | |
Raymond Hettinger | 06d8cf8 | 2005-07-31 15:36:06 +0000 | [diff] [blame] | 1242 | while ((key = PyIter_Next(it)) != NULL) { |
Raymond Hettinger | f31e175 | 2006-12-08 03:17:18 +0000 | [diff] [blame] | 1243 | int rv; |
| 1244 | setentry entry; |
| 1245 | long hash = PyObject_Hash(key); |
| 1246 | |
| 1247 | if (hash == -1) { |
| 1248 | Py_DECREF(it); |
| 1249 | Py_DECREF(result); |
| 1250 | Py_DECREF(key); |
| 1251 | return NULL; |
| 1252 | } |
| 1253 | entry.hash = hash; |
| 1254 | entry.key = key; |
| 1255 | rv = set_contains_entry(so, &entry); |
Raymond Hettinger | c563a1c | 2006-09-07 02:42:48 +0000 | [diff] [blame] | 1256 | if (rv == -1) { |
| 1257 | Py_DECREF(it); |
| 1258 | Py_DECREF(result); |
| 1259 | Py_DECREF(key); |
| 1260 | return NULL; |
| 1261 | } |
| 1262 | if (rv) { |
Raymond Hettinger | f31e175 | 2006-12-08 03:17:18 +0000 | [diff] [blame] | 1263 | if (set_add_entry(result, &entry) == -1) { |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1264 | Py_DECREF(it); |
| 1265 | Py_DECREF(result); |
Raymond Hettinger | 06d8cf8 | 2005-07-31 15:36:06 +0000 | [diff] [blame] | 1266 | Py_DECREF(key); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1267 | return NULL; |
| 1268 | } |
| 1269 | } |
Raymond Hettinger | 06d8cf8 | 2005-07-31 15:36:06 +0000 | [diff] [blame] | 1270 | Py_DECREF(key); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1271 | } |
| 1272 | Py_DECREF(it); |
| 1273 | if (PyErr_Occurred()) { |
| 1274 | Py_DECREF(result); |
| 1275 | return NULL; |
| 1276 | } |
| 1277 | return (PyObject *)result; |
| 1278 | } |
| 1279 | |
| 1280 | PyDoc_STRVAR(intersection_doc, |
| 1281 | "Return the intersection of two sets as a new set.\n\ |
| 1282 | \n\ |
| 1283 | (i.e. all elements that are in both sets.)"); |
| 1284 | |
| 1285 | static PyObject * |
| 1286 | set_intersection_update(PySetObject *so, PyObject *other) |
| 1287 | { |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1288 | PyObject *tmp; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1289 | |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1290 | tmp = set_intersection(so, other); |
| 1291 | if (tmp == NULL) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1292 | return NULL; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1293 | set_swap_bodies(so, (PySetObject *)tmp); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1294 | Py_DECREF(tmp); |
| 1295 | Py_RETURN_NONE; |
| 1296 | } |
| 1297 | |
| 1298 | PyDoc_STRVAR(intersection_update_doc, |
| 1299 | "Update a set with the intersection of itself and another."); |
| 1300 | |
| 1301 | static PyObject * |
| 1302 | set_and(PySetObject *so, PyObject *other) |
| 1303 | { |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 1304 | if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) { |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1305 | Py_INCREF(Py_NotImplemented); |
| 1306 | return Py_NotImplemented; |
| 1307 | } |
| 1308 | return set_intersection(so, other); |
| 1309 | } |
| 1310 | |
| 1311 | static PyObject * |
| 1312 | set_iand(PySetObject *so, PyObject *other) |
| 1313 | { |
| 1314 | PyObject *result; |
| 1315 | |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 1316 | if (!PyAnySet_Check(other)) { |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1317 | Py_INCREF(Py_NotImplemented); |
| 1318 | return Py_NotImplemented; |
| 1319 | } |
| 1320 | result = set_intersection_update(so, other); |
| 1321 | if (result == NULL) |
| 1322 | return NULL; |
| 1323 | Py_DECREF(result); |
| 1324 | Py_INCREF(so); |
| 1325 | return (PyObject *)so; |
| 1326 | } |
| 1327 | |
Raymond Hettinger | 1760c8a | 2007-11-08 02:52:43 +0000 | [diff] [blame] | 1328 | static PyObject * |
| 1329 | set_isdisjoint(PySetObject *so, PyObject *other) |
| 1330 | { |
| 1331 | PyObject *key, *it, *tmp; |
| 1332 | |
| 1333 | if ((PyObject *)so == other) { |
| 1334 | if (PySet_GET_SIZE(so) == 0) |
| 1335 | Py_RETURN_TRUE; |
| 1336 | else |
| 1337 | Py_RETURN_FALSE; |
| 1338 | } |
| 1339 | |
| 1340 | if (PyAnySet_CheckExact(other)) { |
| 1341 | Py_ssize_t pos = 0; |
| 1342 | setentry *entry; |
| 1343 | |
| 1344 | if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) { |
| 1345 | tmp = (PyObject *)so; |
| 1346 | so = (PySetObject *)other; |
| 1347 | other = tmp; |
| 1348 | } |
| 1349 | while (set_next((PySetObject *)other, &pos, &entry)) { |
| 1350 | int rv = set_contains_entry(so, entry); |
| 1351 | if (rv == -1) |
| 1352 | return NULL; |
| 1353 | if (rv) |
| 1354 | Py_RETURN_FALSE; |
| 1355 | } |
| 1356 | Py_RETURN_TRUE; |
| 1357 | } |
| 1358 | |
| 1359 | it = PyObject_GetIter(other); |
| 1360 | if (it == NULL) |
| 1361 | return NULL; |
| 1362 | |
| 1363 | while ((key = PyIter_Next(it)) != NULL) { |
| 1364 | int rv; |
| 1365 | setentry entry; |
| 1366 | long hash = PyObject_Hash(key); |
| 1367 | |
Raymond Hettinger | 1760c8a | 2007-11-08 02:52:43 +0000 | [diff] [blame] | 1368 | if (hash == -1) { |
Raymond Hettinger | e8d58ba | 2007-11-08 18:47:51 +0000 | [diff] [blame] | 1369 | Py_DECREF(key); |
Raymond Hettinger | 1760c8a | 2007-11-08 02:52:43 +0000 | [diff] [blame] | 1370 | Py_DECREF(it); |
| 1371 | return NULL; |
| 1372 | } |
| 1373 | entry.hash = hash; |
| 1374 | entry.key = key; |
| 1375 | rv = set_contains_entry(so, &entry); |
Raymond Hettinger | e8d58ba | 2007-11-08 18:47:51 +0000 | [diff] [blame] | 1376 | Py_DECREF(key); |
Raymond Hettinger | 1760c8a | 2007-11-08 02:52:43 +0000 | [diff] [blame] | 1377 | if (rv == -1) { |
| 1378 | Py_DECREF(it); |
| 1379 | return NULL; |
| 1380 | } |
| 1381 | if (rv) { |
| 1382 | Py_DECREF(it); |
| 1383 | Py_RETURN_FALSE; |
| 1384 | } |
| 1385 | } |
| 1386 | Py_DECREF(it); |
| 1387 | if (PyErr_Occurred()) |
| 1388 | return NULL; |
| 1389 | Py_RETURN_TRUE; |
| 1390 | } |
| 1391 | |
| 1392 | PyDoc_STRVAR(isdisjoint_doc, |
| 1393 | "Return True if two sets have a null intersection."); |
| 1394 | |
Neal Norwitz | 6576bd8 | 2005-11-13 18:41:28 +0000 | [diff] [blame] | 1395 | static int |
Raymond Hettinger | c991db2 | 2005-08-11 07:58:45 +0000 | [diff] [blame] | 1396 | set_difference_update_internal(PySetObject *so, PyObject *other) |
| 1397 | { |
| 1398 | if ((PyObject *)so == other) |
| 1399 | return set_clear_internal(so); |
| 1400 | |
Raymond Hettinger | 0e7a632 | 2007-02-08 00:50:39 +0000 | [diff] [blame] | 1401 | if (PyAnySet_CheckExact(other)) { |
Raymond Hettinger | c991db2 | 2005-08-11 07:58:45 +0000 | [diff] [blame] | 1402 | setentry *entry; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1403 | Py_ssize_t pos = 0; |
Raymond Hettinger | c991db2 | 2005-08-11 07:58:45 +0000 | [diff] [blame] | 1404 | |
| 1405 | while (set_next((PySetObject *)other, &pos, &entry)) |
Raymond Hettinger | c563a1c | 2006-09-07 02:42:48 +0000 | [diff] [blame] | 1406 | if (set_discard_entry(so, entry) == -1) |
| 1407 | return -1; |
Raymond Hettinger | c991db2 | 2005-08-11 07:58:45 +0000 | [diff] [blame] | 1408 | } else { |
| 1409 | PyObject *key, *it; |
| 1410 | it = PyObject_GetIter(other); |
| 1411 | if (it == NULL) |
| 1412 | return -1; |
| 1413 | |
| 1414 | while ((key = PyIter_Next(it)) != NULL) { |
| 1415 | if (set_discard_key(so, key) == -1) { |
| 1416 | Py_DECREF(it); |
| 1417 | Py_DECREF(key); |
| 1418 | return -1; |
| 1419 | } |
| 1420 | Py_DECREF(key); |
| 1421 | } |
| 1422 | Py_DECREF(it); |
| 1423 | if (PyErr_Occurred()) |
| 1424 | return -1; |
| 1425 | } |
| 1426 | /* If more than 1/5 are dummies, then resize them away. */ |
| 1427 | if ((so->fill - so->used) * 5 < so->mask) |
| 1428 | return 0; |
| 1429 | return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4); |
| 1430 | } |
| 1431 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1432 | static PyObject * |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1433 | set_difference_update(PySetObject *so, PyObject *other) |
| 1434 | { |
Raymond Hettinger | c991db2 | 2005-08-11 07:58:45 +0000 | [diff] [blame] | 1435 | if (set_difference_update_internal(so, other) != -1) |
Raymond Hettinger | bc841a1 | 2005-08-07 13:02:53 +0000 | [diff] [blame] | 1436 | Py_RETURN_NONE; |
Raymond Hettinger | c991db2 | 2005-08-11 07:58:45 +0000 | [diff] [blame] | 1437 | return NULL; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1438 | } |
| 1439 | |
| 1440 | PyDoc_STRVAR(difference_update_doc, |
| 1441 | "Remove all elements of another set from this set."); |
| 1442 | |
| 1443 | static PyObject * |
Raymond Hettinger | fb4e33a | 2003-12-15 13:23:55 +0000 | [diff] [blame] | 1444 | set_difference(PySetObject *so, PyObject *other) |
| 1445 | { |
Raymond Hettinger | c991db2 | 2005-08-11 07:58:45 +0000 | [diff] [blame] | 1446 | PyObject *result; |
| 1447 | setentry *entry; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1448 | Py_ssize_t pos = 0; |
Raymond Hettinger | fb4e33a | 2003-12-15 13:23:55 +0000 | [diff] [blame] | 1449 | |
Raymond Hettinger | 0e7a632 | 2007-02-08 00:50:39 +0000 | [diff] [blame] | 1450 | if (!PyAnySet_CheckExact(other) && !PyDict_CheckExact(other)) { |
Raymond Hettinger | fb4e33a | 2003-12-15 13:23:55 +0000 | [diff] [blame] | 1451 | result = set_copy(so); |
| 1452 | if (result == NULL) |
Raymond Hettinger | c991db2 | 2005-08-11 07:58:45 +0000 | [diff] [blame] | 1453 | return NULL; |
| 1454 | if (set_difference_update_internal((PySetObject *)result, other) != -1) |
Raymond Hettinger | fb4e33a | 2003-12-15 13:23:55 +0000 | [diff] [blame] | 1455 | return result; |
Raymond Hettinger | fb4e33a | 2003-12-15 13:23:55 +0000 | [diff] [blame] | 1456 | Py_DECREF(result); |
| 1457 | return NULL; |
| 1458 | } |
| 1459 | |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 1460 | result = make_new_set(Py_TYPE(so), NULL); |
Raymond Hettinger | fb4e33a | 2003-12-15 13:23:55 +0000 | [diff] [blame] | 1461 | if (result == NULL) |
| 1462 | return NULL; |
Raymond Hettinger | fb4e33a | 2003-12-15 13:23:55 +0000 | [diff] [blame] | 1463 | |
Raymond Hettinger | db67aef | 2007-02-01 21:02:59 +0000 | [diff] [blame] | 1464 | if (PyDict_CheckExact(other)) { |
Raymond Hettinger | c991db2 | 2005-08-11 07:58:45 +0000 | [diff] [blame] | 1465 | while (set_next(so, &pos, &entry)) { |
| 1466 | setentry entrycopy; |
| 1467 | entrycopy.hash = entry->hash; |
| 1468 | entrycopy.key = entry->key; |
Raymond Hettinger | d6fc72a | 2007-02-19 02:03:19 +0000 | [diff] [blame] | 1469 | if (!_PyDict_Contains(other, entry->key, entry->hash)) { |
Raymond Hettinger | c563a1c | 2006-09-07 02:42:48 +0000 | [diff] [blame] | 1470 | if (set_add_entry((PySetObject *)result, &entrycopy) == -1) { |
| 1471 | Py_DECREF(result); |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1472 | return NULL; |
Raymond Hettinger | c563a1c | 2006-09-07 02:42:48 +0000 | [diff] [blame] | 1473 | } |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1474 | } |
| 1475 | } |
| 1476 | return result; |
| 1477 | } |
| 1478 | |
Raymond Hettinger | c991db2 | 2005-08-11 07:58:45 +0000 | [diff] [blame] | 1479 | while (set_next(so, &pos, &entry)) { |
Raymond Hettinger | c563a1c | 2006-09-07 02:42:48 +0000 | [diff] [blame] | 1480 | int rv = set_contains_entry((PySetObject *)other, entry); |
| 1481 | if (rv == -1) { |
| 1482 | Py_DECREF(result); |
| 1483 | return NULL; |
| 1484 | } |
| 1485 | if (!rv) { |
| 1486 | if (set_add_entry((PySetObject *)result, entry) == -1) { |
| 1487 | Py_DECREF(result); |
Raymond Hettinger | fb4e33a | 2003-12-15 13:23:55 +0000 | [diff] [blame] | 1488 | return NULL; |
Raymond Hettinger | c563a1c | 2006-09-07 02:42:48 +0000 | [diff] [blame] | 1489 | } |
Raymond Hettinger | fb4e33a | 2003-12-15 13:23:55 +0000 | [diff] [blame] | 1490 | } |
| 1491 | } |
| 1492 | return result; |
| 1493 | } |
| 1494 | |
| 1495 | PyDoc_STRVAR(difference_doc, |
| 1496 | "Return the difference of two sets as a new set.\n\ |
| 1497 | \n\ |
| 1498 | (i.e. all elements that are in this set but not the other.)"); |
| 1499 | static PyObject * |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1500 | set_sub(PySetObject *so, PyObject *other) |
| 1501 | { |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 1502 | if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) { |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1503 | Py_INCREF(Py_NotImplemented); |
| 1504 | return Py_NotImplemented; |
| 1505 | } |
| 1506 | return set_difference(so, other); |
| 1507 | } |
| 1508 | |
| 1509 | static PyObject * |
| 1510 | set_isub(PySetObject *so, PyObject *other) |
| 1511 | { |
| 1512 | PyObject *result; |
| 1513 | |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 1514 | if (!PyAnySet_Check(other)) { |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1515 | Py_INCREF(Py_NotImplemented); |
| 1516 | return Py_NotImplemented; |
| 1517 | } |
| 1518 | result = set_difference_update(so, other); |
| 1519 | if (result == NULL) |
| 1520 | return NULL; |
| 1521 | Py_DECREF(result); |
| 1522 | Py_INCREF(so); |
| 1523 | return (PyObject *)so; |
| 1524 | } |
| 1525 | |
| 1526 | static PyObject * |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1527 | set_symmetric_difference_update(PySetObject *so, PyObject *other) |
| 1528 | { |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1529 | PySetObject *otherset; |
| 1530 | PyObject *key; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1531 | Py_ssize_t pos = 0; |
Raymond Hettinger | c991db2 | 2005-08-11 07:58:45 +0000 | [diff] [blame] | 1532 | setentry *entry; |
| 1533 | |
| 1534 | if ((PyObject *)so == other) |
| 1535 | return set_clear(so); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1536 | |
Raymond Hettinger | db67aef | 2007-02-01 21:02:59 +0000 | [diff] [blame] | 1537 | if (PyDict_CheckExact(other)) { |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1538 | PyObject *value; |
| 1539 | int rv; |
Raymond Hettinger | d6fc72a | 2007-02-19 02:03:19 +0000 | [diff] [blame] | 1540 | long hash; |
| 1541 | while (_PyDict_Next(other, &pos, &key, &value, &hash)) { |
Raymond Hettinger | f31e175 | 2006-12-08 03:17:18 +0000 | [diff] [blame] | 1542 | setentry an_entry; |
Raymond Hettinger | f31e175 | 2006-12-08 03:17:18 +0000 | [diff] [blame] | 1543 | |
Raymond Hettinger | f31e175 | 2006-12-08 03:17:18 +0000 | [diff] [blame] | 1544 | an_entry.hash = hash; |
| 1545 | an_entry.key = key; |
| 1546 | rv = set_discard_entry(so, &an_entry); |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1547 | if (rv == -1) |
| 1548 | return NULL; |
| 1549 | if (rv == DISCARD_NOTFOUND) { |
Raymond Hettinger | f31e175 | 2006-12-08 03:17:18 +0000 | [diff] [blame] | 1550 | if (set_add_entry(so, &an_entry) == -1) |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1551 | return NULL; |
| 1552 | } |
| 1553 | } |
| 1554 | Py_RETURN_NONE; |
| 1555 | } |
| 1556 | |
Raymond Hettinger | 0e7a632 | 2007-02-08 00:50:39 +0000 | [diff] [blame] | 1557 | if (PyAnySet_CheckExact(other)) { |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1558 | Py_INCREF(other); |
| 1559 | otherset = (PySetObject *)other; |
| 1560 | } else { |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 1561 | otherset = (PySetObject *)make_new_set(Py_TYPE(so), other); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1562 | if (otherset == NULL) |
| 1563 | return NULL; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1564 | } |
| 1565 | |
Raymond Hettinger | c991db2 | 2005-08-11 07:58:45 +0000 | [diff] [blame] | 1566 | while (set_next(otherset, &pos, &entry)) { |
| 1567 | int rv = set_discard_entry(so, entry); |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1568 | if (rv == -1) { |
Neal Norwitz | 04e39ec | 2006-07-17 00:57:15 +0000 | [diff] [blame] | 1569 | Py_DECREF(otherset); |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1570 | return NULL; |
| 1571 | } |
| 1572 | if (rv == DISCARD_NOTFOUND) { |
Raymond Hettinger | c991db2 | 2005-08-11 07:58:45 +0000 | [diff] [blame] | 1573 | if (set_add_entry(so, entry) == -1) { |
Neal Norwitz | 04e39ec | 2006-07-17 00:57:15 +0000 | [diff] [blame] | 1574 | Py_DECREF(otherset); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1575 | return NULL; |
| 1576 | } |
| 1577 | } |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1578 | } |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1579 | Py_DECREF(otherset); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1580 | Py_RETURN_NONE; |
| 1581 | } |
| 1582 | |
| 1583 | PyDoc_STRVAR(symmetric_difference_update_doc, |
| 1584 | "Update a set with the symmetric difference of itself and another."); |
| 1585 | |
| 1586 | static PyObject * |
Raymond Hettinger | f5f41bf | 2003-11-24 02:57:33 +0000 | [diff] [blame] | 1587 | set_symmetric_difference(PySetObject *so, PyObject *other) |
| 1588 | { |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1589 | PyObject *rv; |
| 1590 | PySetObject *otherset; |
Raymond Hettinger | f5f41bf | 2003-11-24 02:57:33 +0000 | [diff] [blame] | 1591 | |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 1592 | otherset = (PySetObject *)make_new_set(Py_TYPE(so), other); |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1593 | if (otherset == NULL) |
Raymond Hettinger | f5f41bf | 2003-11-24 02:57:33 +0000 | [diff] [blame] | 1594 | return NULL; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1595 | rv = set_symmetric_difference_update(otherset, (PyObject *)so); |
| 1596 | if (rv == NULL) |
| 1597 | return NULL; |
| 1598 | Py_DECREF(rv); |
| 1599 | return (PyObject *)otherset; |
Raymond Hettinger | f5f41bf | 2003-11-24 02:57:33 +0000 | [diff] [blame] | 1600 | } |
| 1601 | |
| 1602 | PyDoc_STRVAR(symmetric_difference_doc, |
| 1603 | "Return the symmetric difference of two sets as a new set.\n\ |
| 1604 | \n\ |
| 1605 | (i.e. all elements that are in exactly one of the sets.)"); |
| 1606 | |
| 1607 | static PyObject * |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1608 | set_xor(PySetObject *so, PyObject *other) |
| 1609 | { |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 1610 | if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) { |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1611 | Py_INCREF(Py_NotImplemented); |
| 1612 | return Py_NotImplemented; |
| 1613 | } |
| 1614 | return set_symmetric_difference(so, other); |
| 1615 | } |
| 1616 | |
| 1617 | static PyObject * |
| 1618 | set_ixor(PySetObject *so, PyObject *other) |
| 1619 | { |
| 1620 | PyObject *result; |
| 1621 | |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 1622 | if (!PyAnySet_Check(other)) { |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1623 | Py_INCREF(Py_NotImplemented); |
| 1624 | return Py_NotImplemented; |
| 1625 | } |
| 1626 | result = set_symmetric_difference_update(so, other); |
| 1627 | if (result == NULL) |
| 1628 | return NULL; |
| 1629 | Py_DECREF(result); |
| 1630 | Py_INCREF(so); |
| 1631 | return (PyObject *)so; |
| 1632 | } |
| 1633 | |
| 1634 | static PyObject * |
| 1635 | set_issubset(PySetObject *so, PyObject *other) |
| 1636 | { |
Raymond Hettinger | ed6c1ef | 2005-08-13 08:28:03 +0000 | [diff] [blame] | 1637 | setentry *entry; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1638 | Py_ssize_t pos = 0; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1639 | |
Raymond Hettinger | 0e7a632 | 2007-02-08 00:50:39 +0000 | [diff] [blame] | 1640 | if (!PyAnySet_CheckExact(other)) { |
Raymond Hettinger | ed6c1ef | 2005-08-13 08:28:03 +0000 | [diff] [blame] | 1641 | PyObject *tmp, *result; |
Raymond Hettinger | 3fbec70 | 2003-11-21 07:56:36 +0000 | [diff] [blame] | 1642 | tmp = make_new_set(&PySet_Type, other); |
| 1643 | if (tmp == NULL) |
| 1644 | return NULL; |
| 1645 | result = set_issubset(so, tmp); |
| 1646 | Py_DECREF(tmp); |
| 1647 | return result; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1648 | } |
Raymond Hettinger | beb3101 | 2005-08-16 03:47:52 +0000 | [diff] [blame] | 1649 | if (PySet_GET_SIZE(so) > PySet_GET_SIZE(other)) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1650 | Py_RETURN_FALSE; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1651 | |
Raymond Hettinger | ed6c1ef | 2005-08-13 08:28:03 +0000 | [diff] [blame] | 1652 | while (set_next(so, &pos, &entry)) { |
Raymond Hettinger | c563a1c | 2006-09-07 02:42:48 +0000 | [diff] [blame] | 1653 | int rv = set_contains_entry((PySetObject *)other, entry); |
| 1654 | if (rv == -1) |
| 1655 | return NULL; |
| 1656 | if (!rv) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1657 | Py_RETURN_FALSE; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1658 | } |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1659 | Py_RETURN_TRUE; |
| 1660 | } |
| 1661 | |
| 1662 | PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set."); |
| 1663 | |
| 1664 | static PyObject * |
| 1665 | set_issuperset(PySetObject *so, PyObject *other) |
| 1666 | { |
Raymond Hettinger | 3fbec70 | 2003-11-21 07:56:36 +0000 | [diff] [blame] | 1667 | PyObject *tmp, *result; |
| 1668 | |
Raymond Hettinger | 0e7a632 | 2007-02-08 00:50:39 +0000 | [diff] [blame] | 1669 | if (!PyAnySet_CheckExact(other)) { |
Raymond Hettinger | 3fbec70 | 2003-11-21 07:56:36 +0000 | [diff] [blame] | 1670 | tmp = make_new_set(&PySet_Type, other); |
| 1671 | if (tmp == NULL) |
| 1672 | return NULL; |
| 1673 | result = set_issuperset(so, tmp); |
| 1674 | Py_DECREF(tmp); |
| 1675 | return result; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1676 | } |
| 1677 | return set_issubset((PySetObject *)other, (PyObject *)so); |
| 1678 | } |
| 1679 | |
| 1680 | PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set."); |
| 1681 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1682 | static PyObject * |
| 1683 | set_richcompare(PySetObject *v, PyObject *w, int op) |
| 1684 | { |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1685 | PyObject *r1, *r2; |
| 1686 | |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 1687 | if(!PyAnySet_Check(w)) { |
| 1688 | if (op == Py_EQ) |
| 1689 | Py_RETURN_FALSE; |
| 1690 | if (op == Py_NE) |
| 1691 | Py_RETURN_TRUE; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1692 | PyErr_SetString(PyExc_TypeError, "can only compare to a set"); |
| 1693 | return NULL; |
| 1694 | } |
| 1695 | switch (op) { |
| 1696 | case Py_EQ: |
Raymond Hettinger | beb3101 | 2005-08-16 03:47:52 +0000 | [diff] [blame] | 1697 | if (PySet_GET_SIZE(v) != PySet_GET_SIZE(w)) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1698 | Py_RETURN_FALSE; |
Raymond Hettinger | 9c1491f | 2005-08-24 00:24:40 +0000 | [diff] [blame] | 1699 | if (v->hash != -1 && |
| 1700 | ((PySetObject *)w)->hash != -1 && |
| 1701 | v->hash != ((PySetObject *)w)->hash) |
| 1702 | Py_RETURN_FALSE; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1703 | return set_issubset(v, w); |
| 1704 | case Py_NE: |
Raymond Hettinger | 9c1491f | 2005-08-24 00:24:40 +0000 | [diff] [blame] | 1705 | r1 = set_richcompare(v, w, Py_EQ); |
| 1706 | if (r1 == NULL) |
| 1707 | return NULL; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1708 | r2 = PyBool_FromLong(PyObject_Not(r1)); |
| 1709 | Py_DECREF(r1); |
| 1710 | return r2; |
| 1711 | case Py_LE: |
| 1712 | return set_issubset(v, w); |
| 1713 | case Py_GE: |
| 1714 | return set_issuperset(v, w); |
| 1715 | case Py_LT: |
Raymond Hettinger | beb3101 | 2005-08-16 03:47:52 +0000 | [diff] [blame] | 1716 | if (PySet_GET_SIZE(v) >= PySet_GET_SIZE(w)) |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1717 | Py_RETURN_FALSE; |
| 1718 | return set_issubset(v, w); |
| 1719 | case Py_GT: |
Raymond Hettinger | beb3101 | 2005-08-16 03:47:52 +0000 | [diff] [blame] | 1720 | if (PySet_GET_SIZE(v) <= PySet_GET_SIZE(w)) |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1721 | Py_RETURN_FALSE; |
| 1722 | return set_issuperset(v, w); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1723 | } |
| 1724 | Py_INCREF(Py_NotImplemented); |
| 1725 | return Py_NotImplemented; |
| 1726 | } |
| 1727 | |
Raymond Hettinger | ed6c1ef | 2005-08-13 08:28:03 +0000 | [diff] [blame] | 1728 | static int |
Georg Brandl | 347b300 | 2006-03-30 11:57:00 +0000 | [diff] [blame] | 1729 | set_nocmp(PyObject *self, PyObject *other) |
Raymond Hettinger | ed6c1ef | 2005-08-13 08:28:03 +0000 | [diff] [blame] | 1730 | { |
| 1731 | PyErr_SetString(PyExc_TypeError, "cannot compare sets using cmp()"); |
| 1732 | return -1; |
| 1733 | } |
| 1734 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1735 | static PyObject * |
Raymond Hettinger | 06d8cf8 | 2005-07-31 15:36:06 +0000 | [diff] [blame] | 1736 | set_add(PySetObject *so, PyObject *key) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1737 | { |
Raymond Hettinger | c991db2 | 2005-08-11 07:58:45 +0000 | [diff] [blame] | 1738 | if (set_add_key(so, key) == -1) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1739 | return NULL; |
Raymond Hettinger | 438e02d | 2003-12-13 19:38:47 +0000 | [diff] [blame] | 1740 | Py_RETURN_NONE; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1741 | } |
| 1742 | |
| 1743 | PyDoc_STRVAR(add_doc, |
| 1744 | "Add an element to a set.\n\ |
| 1745 | \n\ |
| 1746 | This has no effect if the element is already present."); |
| 1747 | |
Raymond Hettinger | ce8185e | 2005-08-13 09:28:48 +0000 | [diff] [blame] | 1748 | static int |
| 1749 | set_contains(PySetObject *so, PyObject *key) |
| 1750 | { |
| 1751 | PyObject *tmpkey; |
| 1752 | int rv; |
| 1753 | |
| 1754 | rv = set_contains_key(so, key); |
| 1755 | if (rv == -1) { |
| 1756 | if (!PyAnySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError)) |
| 1757 | return -1; |
| 1758 | PyErr_Clear(); |
| 1759 | tmpkey = make_new_set(&PyFrozenSet_Type, NULL); |
| 1760 | if (tmpkey == NULL) |
| 1761 | return -1; |
| 1762 | set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key); |
| 1763 | rv = set_contains(so, tmpkey); |
| 1764 | set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key); |
| 1765 | Py_DECREF(tmpkey); |
| 1766 | } |
| 1767 | return rv; |
| 1768 | } |
| 1769 | |
| 1770 | static PyObject * |
| 1771 | set_direct_contains(PySetObject *so, PyObject *key) |
| 1772 | { |
| 1773 | long result; |
| 1774 | |
| 1775 | result = set_contains(so, key); |
| 1776 | if (result == -1) |
| 1777 | return NULL; |
| 1778 | return PyBool_FromLong(result); |
| 1779 | } |
| 1780 | |
| 1781 | PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x."); |
| 1782 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1783 | static PyObject * |
Raymond Hettinger | 06d8cf8 | 2005-07-31 15:36:06 +0000 | [diff] [blame] | 1784 | set_remove(PySetObject *so, PyObject *key) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1785 | { |
Raymond Hettinger | 06d8cf8 | 2005-07-31 15:36:06 +0000 | [diff] [blame] | 1786 | PyObject *tmpkey, *result; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1787 | int rv; |
Raymond Hettinger | bfd334a | 2003-11-22 03:55:23 +0000 | [diff] [blame] | 1788 | |
Raymond Hettinger | b02c35e | 2005-08-12 20:48:39 +0000 | [diff] [blame] | 1789 | rv = set_discard_key(so, key); |
| 1790 | if (rv == -1) { |
| 1791 | if (!PyAnySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError)) |
| 1792 | return NULL; |
| 1793 | PyErr_Clear(); |
Raymond Hettinger | 06d8cf8 | 2005-07-31 15:36:06 +0000 | [diff] [blame] | 1794 | tmpkey = make_new_set(&PyFrozenSet_Type, NULL); |
| 1795 | if (tmpkey == NULL) |
Raymond Hettinger | bfd334a | 2003-11-22 03:55:23 +0000 | [diff] [blame] | 1796 | return NULL; |
Raymond Hettinger | b02c35e | 2005-08-12 20:48:39 +0000 | [diff] [blame] | 1797 | set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key); |
Raymond Hettinger | 06d8cf8 | 2005-07-31 15:36:06 +0000 | [diff] [blame] | 1798 | result = set_remove(so, tmpkey); |
Raymond Hettinger | b02c35e | 2005-08-12 20:48:39 +0000 | [diff] [blame] | 1799 | set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key); |
Raymond Hettinger | 06d8cf8 | 2005-07-31 15:36:06 +0000 | [diff] [blame] | 1800 | Py_DECREF(tmpkey); |
Raymond Hettinger | 0deab62 | 2003-12-13 18:53:18 +0000 | [diff] [blame] | 1801 | return result; |
Raymond Hettinger | b02c35e | 2005-08-12 20:48:39 +0000 | [diff] [blame] | 1802 | } else if (rv == DISCARD_NOTFOUND) { |
Raymond Hettinger | 9c14ffb | 2006-12-08 04:57:50 +0000 | [diff] [blame] | 1803 | set_key_error(key); |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1804 | return NULL; |
| 1805 | } |
Raymond Hettinger | 438e02d | 2003-12-13 19:38:47 +0000 | [diff] [blame] | 1806 | Py_RETURN_NONE; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1807 | } |
| 1808 | |
| 1809 | PyDoc_STRVAR(remove_doc, |
| 1810 | "Remove an element from a set; it must be a member.\n\ |
| 1811 | \n\ |
| 1812 | If the element is not a member, raise a KeyError."); |
| 1813 | |
| 1814 | static PyObject * |
Raymond Hettinger | 06d8cf8 | 2005-07-31 15:36:06 +0000 | [diff] [blame] | 1815 | set_discard(PySetObject *so, PyObject *key) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1816 | { |
Raymond Hettinger | 06d8cf8 | 2005-07-31 15:36:06 +0000 | [diff] [blame] | 1817 | PyObject *tmpkey, *result; |
Raymond Hettinger | b02c35e | 2005-08-12 20:48:39 +0000 | [diff] [blame] | 1818 | int rv; |
Raymond Hettinger | 0deab62 | 2003-12-13 18:53:18 +0000 | [diff] [blame] | 1819 | |
Raymond Hettinger | b02c35e | 2005-08-12 20:48:39 +0000 | [diff] [blame] | 1820 | rv = set_discard_key(so, key); |
| 1821 | if (rv == -1) { |
| 1822 | if (!PyAnySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError)) |
| 1823 | return NULL; |
| 1824 | PyErr_Clear(); |
Raymond Hettinger | 06d8cf8 | 2005-07-31 15:36:06 +0000 | [diff] [blame] | 1825 | tmpkey = make_new_set(&PyFrozenSet_Type, NULL); |
| 1826 | if (tmpkey == NULL) |
Raymond Hettinger | 0deab62 | 2003-12-13 18:53:18 +0000 | [diff] [blame] | 1827 | return NULL; |
Raymond Hettinger | b02c35e | 2005-08-12 20:48:39 +0000 | [diff] [blame] | 1828 | set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key); |
Raymond Hettinger | 06d8cf8 | 2005-07-31 15:36:06 +0000 | [diff] [blame] | 1829 | result = set_discard(so, tmpkey); |
Raymond Hettinger | b02c35e | 2005-08-12 20:48:39 +0000 | [diff] [blame] | 1830 | set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key); |
Raymond Hettinger | 06d8cf8 | 2005-07-31 15:36:06 +0000 | [diff] [blame] | 1831 | Py_DECREF(tmpkey); |
Raymond Hettinger | 0deab62 | 2003-12-13 18:53:18 +0000 | [diff] [blame] | 1832 | return result; |
| 1833 | } |
Raymond Hettinger | 438e02d | 2003-12-13 19:38:47 +0000 | [diff] [blame] | 1834 | Py_RETURN_NONE; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1835 | } |
| 1836 | |
| 1837 | PyDoc_STRVAR(discard_doc, |
| 1838 | "Remove an element from a set if it is a member.\n\ |
| 1839 | \n\ |
| 1840 | If the element is not a member, do nothing."); |
| 1841 | |
| 1842 | static PyObject * |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1843 | set_reduce(PySetObject *so) |
| 1844 | { |
Raymond Hettinger | 15056a5 | 2004-11-09 07:25:31 +0000 | [diff] [blame] | 1845 | PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1846 | |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1847 | keys = PySequence_List((PyObject *)so); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1848 | if (keys == NULL) |
| 1849 | goto done; |
| 1850 | args = PyTuple_Pack(1, keys); |
| 1851 | if (args == NULL) |
| 1852 | goto done; |
Raymond Hettinger | 15056a5 | 2004-11-09 07:25:31 +0000 | [diff] [blame] | 1853 | dict = PyObject_GetAttrString((PyObject *)so, "__dict__"); |
| 1854 | if (dict == NULL) { |
| 1855 | PyErr_Clear(); |
| 1856 | dict = Py_None; |
| 1857 | Py_INCREF(dict); |
| 1858 | } |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 1859 | result = PyTuple_Pack(3, Py_TYPE(so), args, dict); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1860 | done: |
| 1861 | Py_XDECREF(args); |
| 1862 | Py_XDECREF(keys); |
Raymond Hettinger | 15056a5 | 2004-11-09 07:25:31 +0000 | [diff] [blame] | 1863 | Py_XDECREF(dict); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1864 | return result; |
| 1865 | } |
| 1866 | |
| 1867 | PyDoc_STRVAR(reduce_doc, "Return state information for pickling."); |
| 1868 | |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 1869 | static int |
| 1870 | set_init(PySetObject *self, PyObject *args, PyObject *kwds) |
| 1871 | { |
| 1872 | PyObject *iterable = NULL; |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 1873 | |
| 1874 | if (!PyAnySet_Check(self)) |
| 1875 | return -1; |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 1876 | if (!PyArg_UnpackTuple(args, Py_TYPE(self)->tp_name, 0, 1, &iterable)) |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 1877 | return -1; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1878 | set_clear_internal(self); |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 1879 | self->hash = -1; |
| 1880 | if (iterable == NULL) |
| 1881 | return 0; |
Raymond Hettinger | d794666 | 2005-08-01 21:39:29 +0000 | [diff] [blame] | 1882 | return set_update_internal(self, iterable); |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 1883 | } |
| 1884 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1885 | static PySequenceMethods set_as_sequence = { |
Georg Brandl | 347b300 | 2006-03-30 11:57:00 +0000 | [diff] [blame] | 1886 | set_len, /* sq_length */ |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1887 | 0, /* sq_concat */ |
| 1888 | 0, /* sq_repeat */ |
| 1889 | 0, /* sq_item */ |
| 1890 | 0, /* sq_slice */ |
| 1891 | 0, /* sq_ass_item */ |
| 1892 | 0, /* sq_ass_slice */ |
| 1893 | (objobjproc)set_contains, /* sq_contains */ |
| 1894 | }; |
| 1895 | |
| 1896 | /* set object ********************************************************/ |
| 1897 | |
Raymond Hettinger | c47e01d | 2005-08-16 10:44:15 +0000 | [diff] [blame] | 1898 | #ifdef Py_DEBUG |
| 1899 | static PyObject *test_c_api(PySetObject *so); |
| 1900 | |
| 1901 | PyDoc_STRVAR(test_c_api_doc, "Exercises C API. Returns True.\n\ |
| 1902 | All is well if assertions don't fail."); |
| 1903 | #endif |
| 1904 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1905 | static PyMethodDef set_methods[] = { |
| 1906 | {"add", (PyCFunction)set_add, METH_O, |
| 1907 | add_doc}, |
| 1908 | {"clear", (PyCFunction)set_clear, METH_NOARGS, |
| 1909 | clear_doc}, |
Raymond Hettinger | 0deab62 | 2003-12-13 18:53:18 +0000 | [diff] [blame] | 1910 | {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST, |
Raymond Hettinger | 8f5cdaa | 2003-12-13 11:26:12 +0000 | [diff] [blame] | 1911 | contains_doc}, |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1912 | {"copy", (PyCFunction)set_copy, METH_NOARGS, |
| 1913 | copy_doc}, |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1914 | {"discard", (PyCFunction)set_discard, METH_O, |
| 1915 | discard_doc}, |
| 1916 | {"difference", (PyCFunction)set_difference, METH_O, |
| 1917 | difference_doc}, |
| 1918 | {"difference_update", (PyCFunction)set_difference_update, METH_O, |
| 1919 | difference_update_doc}, |
| 1920 | {"intersection",(PyCFunction)set_intersection, METH_O, |
| 1921 | intersection_doc}, |
| 1922 | {"intersection_update",(PyCFunction)set_intersection_update, METH_O, |
| 1923 | intersection_update_doc}, |
Raymond Hettinger | 1760c8a | 2007-11-08 02:52:43 +0000 | [diff] [blame] | 1924 | {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O, |
| 1925 | isdisjoint_doc}, |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1926 | {"issubset", (PyCFunction)set_issubset, METH_O, |
| 1927 | issubset_doc}, |
| 1928 | {"issuperset", (PyCFunction)set_issuperset, METH_O, |
| 1929 | issuperset_doc}, |
| 1930 | {"pop", (PyCFunction)set_pop, METH_NOARGS, |
| 1931 | pop_doc}, |
| 1932 | {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS, |
| 1933 | reduce_doc}, |
| 1934 | {"remove", (PyCFunction)set_remove, METH_O, |
| 1935 | remove_doc}, |
| 1936 | {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O, |
| 1937 | symmetric_difference_doc}, |
| 1938 | {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O, |
| 1939 | symmetric_difference_update_doc}, |
Raymond Hettinger | c47e01d | 2005-08-16 10:44:15 +0000 | [diff] [blame] | 1940 | #ifdef Py_DEBUG |
| 1941 | {"test_c_api", (PyCFunction)test_c_api, METH_NOARGS, |
| 1942 | test_c_api_doc}, |
| 1943 | #endif |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1944 | {"union", (PyCFunction)set_union, METH_O, |
| 1945 | union_doc}, |
Raymond Hettinger | a38123e | 2003-11-24 22:18:49 +0000 | [diff] [blame] | 1946 | {"update", (PyCFunction)set_update, METH_O, |
| 1947 | update_doc}, |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1948 | {NULL, NULL} /* sentinel */ |
| 1949 | }; |
| 1950 | |
| 1951 | static PyNumberMethods set_as_number = { |
| 1952 | 0, /*nb_add*/ |
| 1953 | (binaryfunc)set_sub, /*nb_subtract*/ |
| 1954 | 0, /*nb_multiply*/ |
| 1955 | 0, /*nb_divide*/ |
| 1956 | 0, /*nb_remainder*/ |
| 1957 | 0, /*nb_divmod*/ |
| 1958 | 0, /*nb_power*/ |
| 1959 | 0, /*nb_negative*/ |
| 1960 | 0, /*nb_positive*/ |
| 1961 | 0, /*nb_absolute*/ |
| 1962 | 0, /*nb_nonzero*/ |
| 1963 | 0, /*nb_invert*/ |
| 1964 | 0, /*nb_lshift*/ |
| 1965 | 0, /*nb_rshift*/ |
| 1966 | (binaryfunc)set_and, /*nb_and*/ |
| 1967 | (binaryfunc)set_xor, /*nb_xor*/ |
| 1968 | (binaryfunc)set_or, /*nb_or*/ |
| 1969 | 0, /*nb_coerce*/ |
| 1970 | 0, /*nb_int*/ |
| 1971 | 0, /*nb_long*/ |
| 1972 | 0, /*nb_float*/ |
| 1973 | 0, /*nb_oct*/ |
| 1974 | 0, /*nb_hex*/ |
| 1975 | 0, /*nb_inplace_add*/ |
| 1976 | (binaryfunc)set_isub, /*nb_inplace_subtract*/ |
| 1977 | 0, /*nb_inplace_multiply*/ |
| 1978 | 0, /*nb_inplace_divide*/ |
| 1979 | 0, /*nb_inplace_remainder*/ |
| 1980 | 0, /*nb_inplace_power*/ |
| 1981 | 0, /*nb_inplace_lshift*/ |
| 1982 | 0, /*nb_inplace_rshift*/ |
| 1983 | (binaryfunc)set_iand, /*nb_inplace_and*/ |
| 1984 | (binaryfunc)set_ixor, /*nb_inplace_xor*/ |
| 1985 | (binaryfunc)set_ior, /*nb_inplace_or*/ |
| 1986 | }; |
| 1987 | |
| 1988 | PyDoc_STRVAR(set_doc, |
| 1989 | "set(iterable) --> set object\n\ |
| 1990 | \n\ |
Andrew M. Kuchling | 52740be | 2006-07-29 15:10:32 +0000 | [diff] [blame] | 1991 | Build an unordered collection of unique elements."); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1992 | |
| 1993 | PyTypeObject PySet_Type = { |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 1994 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1995 | "set", /* tp_name */ |
| 1996 | sizeof(PySetObject), /* tp_basicsize */ |
| 1997 | 0, /* tp_itemsize */ |
| 1998 | /* methods */ |
| 1999 | (destructor)set_dealloc, /* tp_dealloc */ |
| 2000 | (printfunc)set_tp_print, /* tp_print */ |
| 2001 | 0, /* tp_getattr */ |
| 2002 | 0, /* tp_setattr */ |
Georg Brandl | 347b300 | 2006-03-30 11:57:00 +0000 | [diff] [blame] | 2003 | set_nocmp, /* tp_compare */ |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 2004 | (reprfunc)set_repr, /* tp_repr */ |
| 2005 | &set_as_number, /* tp_as_number */ |
| 2006 | &set_as_sequence, /* tp_as_sequence */ |
| 2007 | 0, /* tp_as_mapping */ |
Guido van Rossum | 64c06e3 | 2007-11-22 00:55:51 +0000 | [diff] [blame] | 2008 | 0, /* tp_hash */ |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 2009 | 0, /* tp_call */ |
| 2010 | 0, /* tp_str */ |
| 2011 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 2012 | 0, /* tp_setattro */ |
| 2013 | 0, /* tp_as_buffer */ |
Raymond Hettinger | bb999b5 | 2005-06-18 21:00:26 +0000 | [diff] [blame] | 2014 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES | |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 2015 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 2016 | set_doc, /* tp_doc */ |
Raymond Hettinger | bb999b5 | 2005-06-18 21:00:26 +0000 | [diff] [blame] | 2017 | (traverseproc)set_traverse, /* tp_traverse */ |
Raymond Hettinger | fe889f3 | 2005-08-06 05:43:39 +0000 | [diff] [blame] | 2018 | (inquiry)set_clear_internal, /* tp_clear */ |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 2019 | (richcmpfunc)set_richcompare, /* tp_richcompare */ |
Raymond Hettinger | 691d805 | 2004-05-30 07:26:47 +0000 | [diff] [blame] | 2020 | offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */ |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 2021 | (getiterfunc)set_iter, /* tp_iter */ |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 2022 | 0, /* tp_iternext */ |
| 2023 | set_methods, /* tp_methods */ |
| 2024 | 0, /* tp_members */ |
| 2025 | 0, /* tp_getset */ |
| 2026 | 0, /* tp_base */ |
| 2027 | 0, /* tp_dict */ |
| 2028 | 0, /* tp_descr_get */ |
| 2029 | 0, /* tp_descr_set */ |
| 2030 | 0, /* tp_dictoffset */ |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 2031 | (initproc)set_init, /* tp_init */ |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 2032 | PyType_GenericAlloc, /* tp_alloc */ |
| 2033 | set_new, /* tp_new */ |
Raymond Hettinger | bb999b5 | 2005-06-18 21:00:26 +0000 | [diff] [blame] | 2034 | PyObject_GC_Del, /* tp_free */ |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 2035 | }; |
| 2036 | |
| 2037 | /* frozenset object ********************************************************/ |
| 2038 | |
| 2039 | |
| 2040 | static PyMethodDef frozenset_methods[] = { |
Raymond Hettinger | 0deab62 | 2003-12-13 18:53:18 +0000 | [diff] [blame] | 2041 | {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST, |
Raymond Hettinger | 8f5cdaa | 2003-12-13 11:26:12 +0000 | [diff] [blame] | 2042 | contains_doc}, |
Raymond Hettinger | 49ba4c3 | 2003-11-23 02:49:05 +0000 | [diff] [blame] | 2043 | {"copy", (PyCFunction)frozenset_copy, METH_NOARGS, |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 2044 | copy_doc}, |
Raymond Hettinger | 49ba4c3 | 2003-11-23 02:49:05 +0000 | [diff] [blame] | 2045 | {"difference", (PyCFunction)set_difference, METH_O, |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 2046 | difference_doc}, |
| 2047 | {"intersection",(PyCFunction)set_intersection, METH_O, |
| 2048 | intersection_doc}, |
Raymond Hettinger | 1760c8a | 2007-11-08 02:52:43 +0000 | [diff] [blame] | 2049 | {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O, |
| 2050 | isdisjoint_doc}, |
Raymond Hettinger | 49ba4c3 | 2003-11-23 02:49:05 +0000 | [diff] [blame] | 2051 | {"issubset", (PyCFunction)set_issubset, METH_O, |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 2052 | issubset_doc}, |
Raymond Hettinger | 49ba4c3 | 2003-11-23 02:49:05 +0000 | [diff] [blame] | 2053 | {"issuperset", (PyCFunction)set_issuperset, METH_O, |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 2054 | issuperset_doc}, |
| 2055 | {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS, |
| 2056 | reduce_doc}, |
| 2057 | {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O, |
| 2058 | symmetric_difference_doc}, |
| 2059 | {"union", (PyCFunction)set_union, METH_O, |
| 2060 | union_doc}, |
| 2061 | {NULL, NULL} /* sentinel */ |
| 2062 | }; |
| 2063 | |
| 2064 | static PyNumberMethods frozenset_as_number = { |
| 2065 | 0, /*nb_add*/ |
| 2066 | (binaryfunc)set_sub, /*nb_subtract*/ |
| 2067 | 0, /*nb_multiply*/ |
| 2068 | 0, /*nb_divide*/ |
| 2069 | 0, /*nb_remainder*/ |
| 2070 | 0, /*nb_divmod*/ |
| 2071 | 0, /*nb_power*/ |
| 2072 | 0, /*nb_negative*/ |
| 2073 | 0, /*nb_positive*/ |
| 2074 | 0, /*nb_absolute*/ |
| 2075 | 0, /*nb_nonzero*/ |
| 2076 | 0, /*nb_invert*/ |
| 2077 | 0, /*nb_lshift*/ |
| 2078 | 0, /*nb_rshift*/ |
| 2079 | (binaryfunc)set_and, /*nb_and*/ |
| 2080 | (binaryfunc)set_xor, /*nb_xor*/ |
| 2081 | (binaryfunc)set_or, /*nb_or*/ |
| 2082 | }; |
| 2083 | |
| 2084 | PyDoc_STRVAR(frozenset_doc, |
| 2085 | "frozenset(iterable) --> frozenset object\n\ |
| 2086 | \n\ |
Andrew M. Kuchling | 52740be | 2006-07-29 15:10:32 +0000 | [diff] [blame] | 2087 | Build an immutable unordered collection of unique elements."); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 2088 | |
| 2089 | PyTypeObject PyFrozenSet_Type = { |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 2090 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 2091 | "frozenset", /* tp_name */ |
| 2092 | sizeof(PySetObject), /* tp_basicsize */ |
Raymond Hettinger | a3b11e7 | 2003-12-31 14:08:58 +0000 | [diff] [blame] | 2093 | 0, /* tp_itemsize */ |
| 2094 | /* methods */ |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 2095 | (destructor)set_dealloc, /* tp_dealloc */ |
| 2096 | (printfunc)set_tp_print, /* tp_print */ |
| 2097 | 0, /* tp_getattr */ |
| 2098 | 0, /* tp_setattr */ |
Georg Brandl | 347b300 | 2006-03-30 11:57:00 +0000 | [diff] [blame] | 2099 | set_nocmp, /* tp_compare */ |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 2100 | (reprfunc)set_repr, /* tp_repr */ |
| 2101 | &frozenset_as_number, /* tp_as_number */ |
| 2102 | &set_as_sequence, /* tp_as_sequence */ |
| 2103 | 0, /* tp_as_mapping */ |
| 2104 | frozenset_hash, /* tp_hash */ |
| 2105 | 0, /* tp_call */ |
| 2106 | 0, /* tp_str */ |
| 2107 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 2108 | 0, /* tp_setattro */ |
| 2109 | 0, /* tp_as_buffer */ |
Raymond Hettinger | bb999b5 | 2005-06-18 21:00:26 +0000 | [diff] [blame] | 2110 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES | |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 2111 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 2112 | frozenset_doc, /* tp_doc */ |
Raymond Hettinger | bb999b5 | 2005-06-18 21:00:26 +0000 | [diff] [blame] | 2113 | (traverseproc)set_traverse, /* tp_traverse */ |
Raymond Hettinger | fe889f3 | 2005-08-06 05:43:39 +0000 | [diff] [blame] | 2114 | (inquiry)set_clear_internal, /* tp_clear */ |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 2115 | (richcmpfunc)set_richcompare, /* tp_richcompare */ |
Raymond Hettinger | 691d805 | 2004-05-30 07:26:47 +0000 | [diff] [blame] | 2116 | offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */ |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 2117 | (getiterfunc)set_iter, /* tp_iter */ |
| 2118 | 0, /* tp_iternext */ |
| 2119 | frozenset_methods, /* tp_methods */ |
| 2120 | 0, /* tp_members */ |
| 2121 | 0, /* tp_getset */ |
| 2122 | 0, /* tp_base */ |
| 2123 | 0, /* tp_dict */ |
| 2124 | 0, /* tp_descr_get */ |
| 2125 | 0, /* tp_descr_set */ |
| 2126 | 0, /* tp_dictoffset */ |
| 2127 | 0, /* tp_init */ |
| 2128 | PyType_GenericAlloc, /* tp_alloc */ |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 2129 | frozenset_new, /* tp_new */ |
Raymond Hettinger | bb999b5 | 2005-06-18 21:00:26 +0000 | [diff] [blame] | 2130 | PyObject_GC_Del, /* tp_free */ |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 2131 | }; |
Raymond Hettinger | beb3101 | 2005-08-16 03:47:52 +0000 | [diff] [blame] | 2132 | |
| 2133 | |
| 2134 | /***** C API functions *************************************************/ |
| 2135 | |
| 2136 | PyObject * |
| 2137 | PySet_New(PyObject *iterable) |
| 2138 | { |
| 2139 | return make_new_set(&PySet_Type, iterable); |
| 2140 | } |
| 2141 | |
| 2142 | PyObject * |
| 2143 | PyFrozenSet_New(PyObject *iterable) |
| 2144 | { |
Raymond Hettinger | c47e01d | 2005-08-16 10:44:15 +0000 | [diff] [blame] | 2145 | PyObject *args, *result; |
Raymond Hettinger | beb3101 | 2005-08-16 03:47:52 +0000 | [diff] [blame] | 2146 | |
Raymond Hettinger | c47e01d | 2005-08-16 10:44:15 +0000 | [diff] [blame] | 2147 | if (iterable == NULL) |
| 2148 | args = PyTuple_New(0); |
| 2149 | else |
Raymond Hettinger | beb3101 | 2005-08-16 03:47:52 +0000 | [diff] [blame] | 2150 | args = PyTuple_Pack(1, iterable); |
Raymond Hettinger | c47e01d | 2005-08-16 10:44:15 +0000 | [diff] [blame] | 2151 | if (args == NULL) |
| 2152 | return NULL; |
Raymond Hettinger | beb3101 | 2005-08-16 03:47:52 +0000 | [diff] [blame] | 2153 | result = frozenset_new(&PyFrozenSet_Type, args, NULL); |
Raymond Hettinger | c47e01d | 2005-08-16 10:44:15 +0000 | [diff] [blame] | 2154 | Py_DECREF(args); |
Raymond Hettinger | beb3101 | 2005-08-16 03:47:52 +0000 | [diff] [blame] | 2155 | return result; |
| 2156 | } |
| 2157 | |
Neal Norwitz | 8c49c82 | 2006-03-04 18:41:19 +0000 | [diff] [blame] | 2158 | Py_ssize_t |
Raymond Hettinger | c47e01d | 2005-08-16 10:44:15 +0000 | [diff] [blame] | 2159 | PySet_Size(PyObject *anyset) |
| 2160 | { |
| 2161 | if (!PyAnySet_Check(anyset)) { |
| 2162 | PyErr_BadInternalCall(); |
| 2163 | return -1; |
| 2164 | } |
Raymond Hettinger | 9c1491f | 2005-08-24 00:24:40 +0000 | [diff] [blame] | 2165 | return PySet_GET_SIZE(anyset); |
Raymond Hettinger | c47e01d | 2005-08-16 10:44:15 +0000 | [diff] [blame] | 2166 | } |
| 2167 | |
| 2168 | int |
Barry Warsaw | 176014f | 2006-03-30 22:45:35 +0000 | [diff] [blame] | 2169 | PySet_Clear(PyObject *set) |
| 2170 | { |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 2171 | if (!PyType_IsSubtype(Py_TYPE(set), &PySet_Type)) { |
Barry Warsaw | 176014f | 2006-03-30 22:45:35 +0000 | [diff] [blame] | 2172 | PyErr_BadInternalCall(); |
| 2173 | return -1; |
| 2174 | } |
| 2175 | return set_clear_internal((PySetObject *)set); |
| 2176 | } |
| 2177 | |
| 2178 | int |
Raymond Hettinger | beb3101 | 2005-08-16 03:47:52 +0000 | [diff] [blame] | 2179 | PySet_Contains(PyObject *anyset, PyObject *key) |
| 2180 | { |
| 2181 | if (!PyAnySet_Check(anyset)) { |
| 2182 | PyErr_BadInternalCall(); |
| 2183 | return -1; |
| 2184 | } |
| 2185 | return set_contains_key((PySetObject *)anyset, key); |
| 2186 | } |
| 2187 | |
| 2188 | int |
Raymond Hettinger | c47e01d | 2005-08-16 10:44:15 +0000 | [diff] [blame] | 2189 | PySet_Discard(PyObject *set, PyObject *key) |
Raymond Hettinger | beb3101 | 2005-08-16 03:47:52 +0000 | [diff] [blame] | 2190 | { |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 2191 | if (!PyType_IsSubtype(Py_TYPE(set), &PySet_Type)) { |
Raymond Hettinger | beb3101 | 2005-08-16 03:47:52 +0000 | [diff] [blame] | 2192 | PyErr_BadInternalCall(); |
| 2193 | return -1; |
| 2194 | } |
Raymond Hettinger | c47e01d | 2005-08-16 10:44:15 +0000 | [diff] [blame] | 2195 | return set_discard_key((PySetObject *)set, key); |
Raymond Hettinger | beb3101 | 2005-08-16 03:47:52 +0000 | [diff] [blame] | 2196 | } |
| 2197 | |
| 2198 | int |
| 2199 | PySet_Add(PyObject *set, PyObject *key) |
| 2200 | { |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 2201 | if (!PyType_IsSubtype(Py_TYPE(set), &PySet_Type)) { |
Raymond Hettinger | beb3101 | 2005-08-16 03:47:52 +0000 | [diff] [blame] | 2202 | PyErr_BadInternalCall(); |
| 2203 | return -1; |
| 2204 | } |
| 2205 | return set_add_key((PySetObject *)set, key); |
| 2206 | } |
| 2207 | |
Barry Warsaw | 176014f | 2006-03-30 22:45:35 +0000 | [diff] [blame] | 2208 | int |
Raymond Hettinger | 0bbbfc4 | 2007-03-20 21:27:24 +0000 | [diff] [blame] | 2209 | _PySet_Next(PyObject *set, Py_ssize_t *pos, PyObject **key) |
Barry Warsaw | 176014f | 2006-03-30 22:45:35 +0000 | [diff] [blame] | 2210 | { |
| 2211 | setentry *entry_ptr; |
| 2212 | |
| 2213 | if (!PyAnySet_Check(set)) { |
| 2214 | PyErr_BadInternalCall(); |
| 2215 | return -1; |
| 2216 | } |
| 2217 | if (set_next((PySetObject *)set, pos, &entry_ptr) == 0) |
| 2218 | return 0; |
Raymond Hettinger | 0bbbfc4 | 2007-03-20 21:27:24 +0000 | [diff] [blame] | 2219 | *key = entry_ptr->key; |
| 2220 | return 1; |
| 2221 | } |
| 2222 | |
| 2223 | int |
| 2224 | _PySet_NextEntry(PyObject *set, Py_ssize_t *pos, PyObject **key, long *hash) |
| 2225 | { |
| 2226 | setentry *entry; |
| 2227 | |
| 2228 | if (!PyAnySet_Check(set)) { |
| 2229 | PyErr_BadInternalCall(); |
| 2230 | return -1; |
| 2231 | } |
| 2232 | if (set_next((PySetObject *)set, pos, &entry) == 0) |
| 2233 | return 0; |
| 2234 | *key = entry->key; |
| 2235 | *hash = entry->hash; |
Barry Warsaw | 176014f | 2006-03-30 22:45:35 +0000 | [diff] [blame] | 2236 | return 1; |
| 2237 | } |
| 2238 | |
Raymond Hettinger | beb3101 | 2005-08-16 03:47:52 +0000 | [diff] [blame] | 2239 | PyObject * |
| 2240 | PySet_Pop(PyObject *set) |
| 2241 | { |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 2242 | if (!PyType_IsSubtype(Py_TYPE(set), &PySet_Type)) { |
Raymond Hettinger | beb3101 | 2005-08-16 03:47:52 +0000 | [diff] [blame] | 2243 | PyErr_BadInternalCall(); |
| 2244 | return NULL; |
| 2245 | } |
| 2246 | return set_pop((PySetObject *)set); |
| 2247 | } |
Raymond Hettinger | c47e01d | 2005-08-16 10:44:15 +0000 | [diff] [blame] | 2248 | |
Barry Warsaw | 176014f | 2006-03-30 22:45:35 +0000 | [diff] [blame] | 2249 | int |
| 2250 | _PySet_Update(PyObject *set, PyObject *iterable) |
| 2251 | { |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 2252 | if (!PyType_IsSubtype(Py_TYPE(set), &PySet_Type)) { |
Barry Warsaw | 176014f | 2006-03-30 22:45:35 +0000 | [diff] [blame] | 2253 | PyErr_BadInternalCall(); |
| 2254 | return -1; |
| 2255 | } |
| 2256 | return set_update_internal((PySetObject *)set, iterable); |
| 2257 | } |
Raymond Hettinger | c47e01d | 2005-08-16 10:44:15 +0000 | [diff] [blame] | 2258 | |
| 2259 | #ifdef Py_DEBUG |
| 2260 | |
| 2261 | /* Test code to be called with any three element set. |
| 2262 | Returns True and original set is restored. */ |
| 2263 | |
| 2264 | #define assertRaises(call_return_value, exception) \ |
| 2265 | do { \ |
| 2266 | assert(call_return_value); \ |
| 2267 | assert(PyErr_ExceptionMatches(exception)); \ |
| 2268 | PyErr_Clear(); \ |
| 2269 | } while(0) |
| 2270 | |
| 2271 | static PyObject * |
| 2272 | test_c_api(PySetObject *so) |
| 2273 | { |
Neal Norwitz | 0f2783c | 2006-06-19 05:40:44 +0000 | [diff] [blame] | 2274 | Py_ssize_t count; |
Barry Warsaw | 176014f | 2006-03-30 22:45:35 +0000 | [diff] [blame] | 2275 | char *s; |
| 2276 | Py_ssize_t i; |
Guido van Rossum | 360496d | 2007-05-10 17:20:15 +0000 | [diff] [blame] | 2277 | PyObject *elem=NULL, *dup=NULL, *t, *f, *dup2, *x; |
Barry Warsaw | 176014f | 2006-03-30 22:45:35 +0000 | [diff] [blame] | 2278 | PyObject *ob = (PyObject *)so; |
Raymond Hettinger | c47e01d | 2005-08-16 10:44:15 +0000 | [diff] [blame] | 2279 | |
| 2280 | /* Verify preconditions and exercise type/size checks */ |
| 2281 | assert(PyAnySet_Check(ob)); |
| 2282 | assert(PyAnySet_CheckExact(ob)); |
| 2283 | assert(!PyFrozenSet_CheckExact(ob)); |
| 2284 | assert(PySet_Size(ob) == 3); |
| 2285 | assert(PySet_GET_SIZE(ob) == 3); |
| 2286 | |
| 2287 | /* Raise TypeError for non-iterable constructor arguments */ |
| 2288 | assertRaises(PySet_New(Py_None) == NULL, PyExc_TypeError); |
| 2289 | assertRaises(PyFrozenSet_New(Py_None) == NULL, PyExc_TypeError); |
| 2290 | |
| 2291 | /* Raise TypeError for unhashable key */ |
| 2292 | dup = PySet_New(ob); |
| 2293 | assertRaises(PySet_Discard(ob, dup) == -1, PyExc_TypeError); |
| 2294 | assertRaises(PySet_Contains(ob, dup) == -1, PyExc_TypeError); |
| 2295 | assertRaises(PySet_Add(ob, dup) == -1, PyExc_TypeError); |
| 2296 | |
| 2297 | /* Exercise successful pop, contains, add, and discard */ |
| 2298 | elem = PySet_Pop(ob); |
| 2299 | assert(PySet_Contains(ob, elem) == 0); |
| 2300 | assert(PySet_GET_SIZE(ob) == 2); |
| 2301 | assert(PySet_Add(ob, elem) == 0); |
| 2302 | assert(PySet_Contains(ob, elem) == 1); |
| 2303 | assert(PySet_GET_SIZE(ob) == 3); |
| 2304 | assert(PySet_Discard(ob, elem) == 1); |
| 2305 | assert(PySet_GET_SIZE(ob) == 2); |
| 2306 | assert(PySet_Discard(ob, elem) == 0); |
| 2307 | assert(PySet_GET_SIZE(ob) == 2); |
| 2308 | |
Barry Warsaw | 176014f | 2006-03-30 22:45:35 +0000 | [diff] [blame] | 2309 | /* Exercise clear */ |
| 2310 | dup2 = PySet_New(dup); |
| 2311 | assert(PySet_Clear(dup2) == 0); |
| 2312 | assert(PySet_Size(dup2) == 0); |
| 2313 | Py_DECREF(dup2); |
| 2314 | |
| 2315 | /* Raise SystemError on clear or update of frozen set */ |
| 2316 | f = PyFrozenSet_New(dup); |
| 2317 | assertRaises(PySet_Clear(f) == -1, PyExc_SystemError); |
| 2318 | assertRaises(_PySet_Update(f, dup) == -1, PyExc_SystemError); |
| 2319 | Py_DECREF(f); |
| 2320 | |
| 2321 | /* Exercise direct iteration */ |
| 2322 | i = 0, count = 0; |
Guido van Rossum | 360496d | 2007-05-10 17:20:15 +0000 | [diff] [blame] | 2323 | while (_PySet_Next((PyObject *)dup, &i, &x)) { |
| 2324 | s = PyString_AsString(x); |
Barry Warsaw | 176014f | 2006-03-30 22:45:35 +0000 | [diff] [blame] | 2325 | assert(s && (s[0] == 'a' || s[0] == 'b' || s[0] == 'c')); |
| 2326 | count++; |
| 2327 | } |
| 2328 | assert(count == 3); |
| 2329 | |
| 2330 | /* Exercise updates */ |
| 2331 | dup2 = PySet_New(NULL); |
| 2332 | assert(_PySet_Update(dup2, dup) == 0); |
| 2333 | assert(PySet_Size(dup2) == 3); |
| 2334 | assert(_PySet_Update(dup2, dup) == 0); |
| 2335 | assert(PySet_Size(dup2) == 3); |
| 2336 | Py_DECREF(dup2); |
| 2337 | |
Raymond Hettinger | c47e01d | 2005-08-16 10:44:15 +0000 | [diff] [blame] | 2338 | /* Raise SystemError when self argument is not a set or frozenset. */ |
| 2339 | t = PyTuple_New(0); |
| 2340 | assertRaises(PySet_Size(t) == -1, PyExc_SystemError); |
| 2341 | assertRaises(PySet_Contains(t, elem) == -1, PyExc_SystemError); |
| 2342 | Py_DECREF(t); |
| 2343 | |
| 2344 | /* Raise SystemError when self argument is not a set. */ |
| 2345 | f = PyFrozenSet_New(dup); |
| 2346 | assert(PySet_Size(f) == 3); |
| 2347 | assert(PyFrozenSet_CheckExact(f)); |
| 2348 | assertRaises(PySet_Add(f, elem) == -1, PyExc_SystemError); |
| 2349 | assertRaises(PySet_Discard(f, elem) == -1, PyExc_SystemError); |
| 2350 | assertRaises(PySet_Pop(f) == NULL, PyExc_SystemError); |
| 2351 | Py_DECREF(f); |
| 2352 | |
| 2353 | /* Raise KeyError when popping from an empty set */ |
Raymond Hettinger | d8e1338 | 2005-08-17 12:27:17 +0000 | [diff] [blame] | 2354 | assert(PyNumber_InPlaceSubtract(ob, ob) == ob); |
| 2355 | Py_DECREF(ob); |
Raymond Hettinger | c47e01d | 2005-08-16 10:44:15 +0000 | [diff] [blame] | 2356 | assert(PySet_GET_SIZE(ob) == 0); |
| 2357 | assertRaises(PySet_Pop(ob) == NULL, PyExc_KeyError); |
| 2358 | |
Raymond Hettinger | d8e1338 | 2005-08-17 12:27:17 +0000 | [diff] [blame] | 2359 | /* Restore the set from the copy using the PyNumber API */ |
| 2360 | assert(PyNumber_InPlaceOr(ob, dup) == ob); |
| 2361 | Py_DECREF(ob); |
Raymond Hettinger | c47e01d | 2005-08-16 10:44:15 +0000 | [diff] [blame] | 2362 | |
| 2363 | /* Verify constructors accept NULL arguments */ |
| 2364 | f = PySet_New(NULL); |
| 2365 | assert(f != NULL); |
| 2366 | assert(PySet_GET_SIZE(f) == 0); |
| 2367 | Py_DECREF(f); |
| 2368 | f = PyFrozenSet_New(NULL); |
| 2369 | assert(f != NULL); |
| 2370 | assert(PyFrozenSet_CheckExact(f)); |
| 2371 | assert(PySet_GET_SIZE(f) == 0); |
| 2372 | Py_DECREF(f); |
| 2373 | |
| 2374 | Py_DECREF(elem); |
| 2375 | Py_DECREF(dup); |
| 2376 | Py_RETURN_TRUE; |
| 2377 | } |
| 2378 | |
Raymond Hettinger | 9bda1d6 | 2005-09-16 07:14:21 +0000 | [diff] [blame] | 2379 | #undef assertRaises |
| 2380 | |
Raymond Hettinger | c47e01d | 2005-08-16 10:44:15 +0000 | [diff] [blame] | 2381 | #endif |