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