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