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