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