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