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