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; |
Yury Selivanov | 7aa5341 | 2015-05-30 10:57:56 -0400 | [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 | } |
Yury Selivanov | 7aa5341 | 2015-05-30 10:57:56 -0400 | [diff] [blame] | 89 | if (entry->hash == -1 && freeslot == NULL) |
| 90 | freeslot = entry; |
Raymond Hettinger | afe8909 | 2013-08-28 20:59:31 -0700 | [diff] [blame] | 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++; |
Yury Selivanov | 7aa5341 | 2015-05-30 10:57:56 -0400 | [diff] [blame] | 95 | if (entry->key == NULL) |
| 96 | goto found_null; |
Raymond Hettinger | c658d85 | 2015-02-02 08:35:00 -0800 | [diff] [blame] | 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]; |
Yury Selivanov | 7aa5341 | 2015-05-30 10:57:56 -0400 | [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: |
Yury Selivanov | 7aa5341 | 2015-05-30 10:57:56 -0400 | [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 | |
Yury Selivanov | 7aa5341 | 2015-05-30 10:57:56 -0400 | [diff] [blame] | 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 | |
| 186 | entry = set_lookkey(so, key, hash); |
| 187 | if (entry == NULL) |
| 188 | return -1; |
| 189 | if (entry->key == NULL) { |
| 190 | /* UNUSED */ |
| 191 | entry->key = key; |
| 192 | entry->hash = hash; |
| 193 | so->fill++; |
| 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; |
Yury Selivanov | 7aa5341 | 2015-05-30 10:57:56 -0400 | [diff] [blame] | 348 | if (entry->key == NULL || entry->key == dummy) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 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 */ |
Raymond Hettinger | a3626bc | 2015-07-15 23:50:14 -0700 | [diff] [blame] | 603 | for (i = 0; i <= other->mask; i++) { |
| 604 | other_entry = &other->table[i]; |
Raymond Hettinger | 1bd8d75 | 2015-05-13 01:26:14 -0700 | [diff] [blame] | 605 | key = other_entry->key; |
| 606 | if (key != NULL && key != dummy) { |
Raymond Hettinger | faf7b7f | 2010-09-03 10:00:50 +0000 | [diff] [blame] | 607 | Py_INCREF(key); |
Raymond Hettinger | 1bd8d75 | 2015-05-13 01:26:14 -0700 | [diff] [blame] | 608 | if (set_insert_key(so, key, other_entry->hash)) { |
Raymond Hettinger | faf7b7f | 2010-09-03 10:00:50 +0000 | [diff] [blame] | 609 | Py_DECREF(key); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 610 | return -1; |
| 611 | } |
| 612 | } |
| 613 | } |
| 614 | return 0; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | static int |
Raymond Hettinger | c991db2 | 2005-08-11 07:58:45 +0000 | [diff] [blame] | 618 | set_contains_entry(PySetObject *so, setentry *entry) |
| 619 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 620 | PyObject *key; |
| 621 | setentry *lu_entry; |
Raymond Hettinger | c991db2 | 2005-08-11 07:58:45 +0000 | [diff] [blame] | 622 | |
Raymond Hettinger | 93035c4 | 2015-01-25 16:12:49 -0800 | [diff] [blame] | 623 | lu_entry = set_lookkey(so, entry->key, entry->hash); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 624 | if (lu_entry == NULL) |
| 625 | return -1; |
| 626 | key = lu_entry->key; |
Yury Selivanov | 7aa5341 | 2015-05-30 10:57:56 -0400 | [diff] [blame] | 627 | return key != NULL && key != dummy; |
Raymond Hettinger | c991db2 | 2005-08-11 07:58:45 +0000 | [diff] [blame] | 628 | } |
| 629 | |
Raymond Hettinger | 8edf27c | 2014-12-26 23:08:58 -0800 | [diff] [blame] | 630 | static int |
| 631 | set_contains_key(PySetObject *so, PyObject *key) |
| 632 | { |
| 633 | setentry entry; |
| 634 | Py_hash_t hash; |
| 635 | |
| 636 | if (!PyUnicode_CheckExact(key) || |
| 637 | (hash = ((PyASCIIObject *) key)->hash) == -1) { |
| 638 | hash = PyObject_Hash(key); |
| 639 | if (hash == -1) |
| 640 | return -1; |
| 641 | } |
| 642 | entry.key = key; |
| 643 | entry.hash = hash; |
| 644 | return set_contains_entry(so, &entry); |
| 645 | } |
| 646 | |
Raymond Hettinger | ce8185e | 2005-08-13 09:28:48 +0000 | [diff] [blame] | 647 | static PyObject * |
| 648 | set_pop(PySetObject *so) |
| 649 | { |
Raymond Hettinger | 9cd6a78 | 2015-01-18 16:06:18 -0800 | [diff] [blame] | 650 | /* Make sure the search finger is in bounds */ |
| 651 | Py_ssize_t i = so->finger & so->mask; |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 652 | setentry *entry; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 653 | PyObject *key; |
Raymond Hettinger | ce8185e | 2005-08-13 09:28:48 +0000 | [diff] [blame] | 654 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 655 | assert (PyAnySet_Check(so)); |
| 656 | if (so->used == 0) { |
| 657 | PyErr_SetString(PyExc_KeyError, "pop from an empty set"); |
| 658 | return NULL; |
| 659 | } |
Raymond Hettinger | ce8185e | 2005-08-13 09:28:48 +0000 | [diff] [blame] | 660 | |
Raymond Hettinger | 1202a47 | 2015-01-18 13:12:42 -0800 | [diff] [blame] | 661 | while ((entry = &so->table[i])->key == NULL || entry->key==dummy) { |
| 662 | i++; |
Raymond Hettinger | ed741d4 | 2015-01-18 21:25:15 -0800 | [diff] [blame] | 663 | if (i > so->mask) |
| 664 | i = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 665 | } |
| 666 | key = entry->key; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 667 | entry->key = dummy; |
Raymond Hettinger | b335dfe | 2015-01-25 16:38:52 -0800 | [diff] [blame] | 668 | entry->hash = -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 669 | so->used--; |
Raymond Hettinger | 1202a47 | 2015-01-18 13:12:42 -0800 | [diff] [blame] | 670 | so->finger = i + 1; /* next place to start */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 671 | return key; |
Raymond Hettinger | ce8185e | 2005-08-13 09:28:48 +0000 | [diff] [blame] | 672 | } |
| 673 | |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 674 | PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.\n\ |
| 675 | Raises KeyError if the set is empty."); |
Raymond Hettinger | ce8185e | 2005-08-13 09:28:48 +0000 | [diff] [blame] | 676 | |
| 677 | static int |
Raymond Hettinger | f408ddf | 2005-08-17 00:27:42 +0000 | [diff] [blame] | 678 | set_traverse(PySetObject *so, visitproc visit, void *arg) |
Raymond Hettinger | ce8185e | 2005-08-13 09:28:48 +0000 | [diff] [blame] | 679 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 680 | Py_ssize_t pos = 0; |
| 681 | setentry *entry; |
Raymond Hettinger | f408ddf | 2005-08-17 00:27:42 +0000 | [diff] [blame] | 682 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 683 | while (set_next(so, &pos, &entry)) |
| 684 | Py_VISIT(entry->key); |
| 685 | return 0; |
Raymond Hettinger | f408ddf | 2005-08-17 00:27:42 +0000 | [diff] [blame] | 686 | } |
| 687 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 688 | static Py_hash_t |
Raymond Hettinger | f408ddf | 2005-08-17 00:27:42 +0000 | [diff] [blame] | 689 | frozenset_hash(PyObject *self) |
| 690 | { |
Raymond Hettinger | 74fc8c4 | 2014-01-05 12:00:31 -0800 | [diff] [blame] | 691 | /* Most of the constants in this hash algorithm are randomly choosen |
| 692 | large primes with "interesting bit patterns" and that passed |
| 693 | tests for good collision statistics on a variety of problematic |
| 694 | datasets such as: |
| 695 | |
| 696 | ps = [] |
| 697 | for r in range(21): |
| 698 | ps += itertools.combinations(range(20), r) |
| 699 | num_distinct_hashes = len({hash(frozenset(s)) for s in ps}) |
| 700 | |
| 701 | */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 702 | PySetObject *so = (PySetObject *)self; |
Gregory P. Smith | 27cbcd6 | 2012-12-10 18:15:46 -0800 | [diff] [blame] | 703 | Py_uhash_t h, hash = 1927868237UL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 704 | setentry *entry; |
| 705 | Py_ssize_t pos = 0; |
Raymond Hettinger | f408ddf | 2005-08-17 00:27:42 +0000 | [diff] [blame] | 706 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 707 | if (so->hash != -1) |
| 708 | return so->hash; |
Raymond Hettinger | f408ddf | 2005-08-17 00:27:42 +0000 | [diff] [blame] | 709 | |
Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 710 | hash *= (Py_uhash_t)PySet_GET_SIZE(self) + 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 711 | while (set_next(so, &pos, &entry)) { |
| 712 | /* Work to increase the bit dispersion for closely spaced hash |
Raymond Hettinger | 06a1c8d | 2015-01-30 18:02:15 -0800 | [diff] [blame] | 713 | values. This is important because some use cases have many |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 714 | combinations of a small number of elements with nearby |
| 715 | hashes so that many distinct combinations collapse to only |
| 716 | a handful of distinct hash values. */ |
Antoine Pitrou | fbb1c61 | 2010-10-23 17:37:54 +0000 | [diff] [blame] | 717 | h = entry->hash; |
Raymond Hettinger | 74fc8c4 | 2014-01-05 12:00:31 -0800 | [diff] [blame] | 718 | hash ^= ((h ^ 89869747UL) ^ (h << 16)) * 3644798167UL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 719 | } |
Raymond Hettinger | 74fc8c4 | 2014-01-05 12:00:31 -0800 | [diff] [blame] | 720 | /* Make the final result spread-out in a different pattern |
Eric V. Smith | 6ba5665 | 2014-01-14 08:15:03 -0500 | [diff] [blame] | 721 | than the algorithm for tuples or other python objects. */ |
Gregory P. Smith | c2176e4 | 2012-12-10 18:32:53 -0800 | [diff] [blame] | 722 | hash = hash * 69069U + 907133923UL; |
Victor Stinner | 12174a5 | 2014-08-15 23:17:38 +0200 | [diff] [blame] | 723 | if (hash == (Py_uhash_t)-1) |
Gregory P. Smith | 27cbcd6 | 2012-12-10 18:15:46 -0800 | [diff] [blame] | 724 | hash = 590923713UL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 725 | so->hash = hash; |
| 726 | return hash; |
Raymond Hettinger | f408ddf | 2005-08-17 00:27:42 +0000 | [diff] [blame] | 727 | } |
| 728 | |
Raymond Hettinger | a9d9936 | 2005-08-05 00:01:15 +0000 | [diff] [blame] | 729 | /***** Set iterator type ***********************************************/ |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 730 | |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 731 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 732 | PyObject_HEAD |
| 733 | PySetObject *si_set; /* Set to NULL when iterator is exhausted */ |
| 734 | Py_ssize_t si_used; |
| 735 | Py_ssize_t si_pos; |
| 736 | Py_ssize_t len; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 737 | } setiterobject; |
| 738 | |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 739 | static void |
| 740 | setiter_dealloc(setiterobject *si) |
| 741 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 742 | Py_XDECREF(si->si_set); |
| 743 | PyObject_GC_Del(si); |
Antoine Pitrou | 7ddda78 | 2009-01-01 15:35:33 +0000 | [diff] [blame] | 744 | } |
| 745 | |
| 746 | static int |
| 747 | setiter_traverse(setiterobject *si, visitproc visit, void *arg) |
| 748 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 749 | Py_VISIT(si->si_set); |
| 750 | return 0; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 751 | } |
| 752 | |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 753 | static PyObject * |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 754 | setiter_len(setiterobject *si) |
| 755 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 756 | Py_ssize_t len = 0; |
| 757 | if (si->si_set != NULL && si->si_used == si->si_set->used) |
| 758 | len = si->len; |
Antoine Pitrou | 671b4d9 | 2010-08-17 17:55:07 +0000 | [diff] [blame] | 759 | return PyLong_FromSsize_t(len); |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 760 | } |
| 761 | |
Armin Rigo | f5b3e36 | 2006-02-11 21:32:43 +0000 | [diff] [blame] | 762 | 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] | 763 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 764 | static PyObject *setiter_iternext(setiterobject *si); |
| 765 | |
| 766 | static PyObject * |
| 767 | setiter_reduce(setiterobject *si) |
| 768 | { |
| 769 | PyObject *list; |
| 770 | setiterobject tmp; |
| 771 | |
| 772 | list = PyList_New(0); |
| 773 | if (!list) |
| 774 | return NULL; |
| 775 | |
Ezio Melotti | 0e1af28 | 2012-09-28 16:43:40 +0300 | [diff] [blame] | 776 | /* copy the iterator state */ |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 777 | tmp = *si; |
| 778 | Py_XINCREF(tmp.si_set); |
Ezio Melotti | 0e1af28 | 2012-09-28 16:43:40 +0300 | [diff] [blame] | 779 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 780 | /* iterate the temporary into a list */ |
| 781 | for(;;) { |
| 782 | PyObject *element = setiter_iternext(&tmp); |
| 783 | if (element) { |
| 784 | if (PyList_Append(list, element)) { |
| 785 | Py_DECREF(element); |
| 786 | Py_DECREF(list); |
| 787 | Py_XDECREF(tmp.si_set); |
| 788 | return NULL; |
| 789 | } |
| 790 | Py_DECREF(element); |
| 791 | } else |
| 792 | break; |
| 793 | } |
| 794 | Py_XDECREF(tmp.si_set); |
| 795 | /* check for error */ |
| 796 | if (tmp.si_set != NULL) { |
| 797 | /* we have an error */ |
| 798 | Py_DECREF(list); |
| 799 | return NULL; |
| 800 | } |
Antoine Pitrou | a701388 | 2012-04-05 00:04:20 +0200 | [diff] [blame] | 801 | return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list); |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 802 | } |
| 803 | |
| 804 | PyDoc_STRVAR(reduce_doc, "Return state information for pickling."); |
| 805 | |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 806 | static PyMethodDef setiter_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 807 | {"__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] | 808 | {"__reduce__", (PyCFunction)setiter_reduce, METH_NOARGS, reduce_doc}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 809 | {NULL, NULL} /* sentinel */ |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 810 | }; |
| 811 | |
Raymond Hettinger | 06d8cf8 | 2005-07-31 15:36:06 +0000 | [diff] [blame] | 812 | static PyObject *setiter_iternext(setiterobject *si) |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 813 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 814 | PyObject *key; |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 815 | Py_ssize_t i, mask; |
| 816 | setentry *entry; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 817 | PySetObject *so = si->si_set; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 818 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 819 | if (so == NULL) |
| 820 | return NULL; |
| 821 | assert (PyAnySet_Check(so)); |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 822 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 823 | if (si->si_used != so->used) { |
| 824 | PyErr_SetString(PyExc_RuntimeError, |
| 825 | "Set changed size during iteration"); |
| 826 | si->si_used = -1; /* Make this state sticky */ |
| 827 | return NULL; |
| 828 | } |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 829 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 830 | i = si->si_pos; |
| 831 | assert(i>=0); |
| 832 | entry = so->table; |
| 833 | mask = so->mask; |
| 834 | while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy)) |
| 835 | i++; |
| 836 | si->si_pos = i+1; |
| 837 | if (i > mask) |
| 838 | goto fail; |
| 839 | si->len--; |
| 840 | key = entry[i].key; |
| 841 | Py_INCREF(key); |
| 842 | return key; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 843 | |
| 844 | fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 845 | Py_DECREF(so); |
| 846 | si->si_set = NULL; |
| 847 | return NULL; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 848 | } |
| 849 | |
Christian Heimes | a22e8bd | 2007-11-29 22:35:39 +0000 | [diff] [blame] | 850 | PyTypeObject PySetIter_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 851 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 852 | "set_iterator", /* tp_name */ |
| 853 | sizeof(setiterobject), /* tp_basicsize */ |
| 854 | 0, /* tp_itemsize */ |
| 855 | /* methods */ |
| 856 | (destructor)setiter_dealloc, /* tp_dealloc */ |
| 857 | 0, /* tp_print */ |
| 858 | 0, /* tp_getattr */ |
| 859 | 0, /* tp_setattr */ |
| 860 | 0, /* tp_reserved */ |
| 861 | 0, /* tp_repr */ |
| 862 | 0, /* tp_as_number */ |
| 863 | 0, /* tp_as_sequence */ |
| 864 | 0, /* tp_as_mapping */ |
| 865 | 0, /* tp_hash */ |
| 866 | 0, /* tp_call */ |
| 867 | 0, /* tp_str */ |
| 868 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 869 | 0, /* tp_setattro */ |
| 870 | 0, /* tp_as_buffer */ |
| 871 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 872 | 0, /* tp_doc */ |
| 873 | (traverseproc)setiter_traverse, /* tp_traverse */ |
| 874 | 0, /* tp_clear */ |
| 875 | 0, /* tp_richcompare */ |
| 876 | 0, /* tp_weaklistoffset */ |
| 877 | PyObject_SelfIter, /* tp_iter */ |
| 878 | (iternextfunc)setiter_iternext, /* tp_iternext */ |
| 879 | setiter_methods, /* tp_methods */ |
| 880 | 0, |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 881 | }; |
| 882 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 883 | static PyObject * |
| 884 | set_iter(PySetObject *so) |
| 885 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 886 | setiterobject *si = PyObject_GC_New(setiterobject, &PySetIter_Type); |
| 887 | if (si == NULL) |
| 888 | return NULL; |
| 889 | Py_INCREF(so); |
| 890 | si->si_set = so; |
| 891 | si->si_used = so->used; |
| 892 | si->si_pos = 0; |
| 893 | si->len = so->used; |
| 894 | _PyObject_GC_TRACK(si); |
| 895 | return (PyObject *)si; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 896 | } |
| 897 | |
Raymond Hettinger | d794666 | 2005-08-01 21:39:29 +0000 | [diff] [blame] | 898 | static int |
Raymond Hettinger | d794666 | 2005-08-01 21:39:29 +0000 | [diff] [blame] | 899 | set_update_internal(PySetObject *so, PyObject *other) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 900 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 901 | PyObject *key, *it; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 902 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 903 | if (PyAnySet_Check(other)) |
| 904 | return set_merge(so, other); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 905 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 906 | if (PyDict_CheckExact(other)) { |
| 907 | PyObject *value; |
| 908 | Py_ssize_t pos = 0; |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 909 | Py_hash_t hash; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 910 | Py_ssize_t dictsize = PyDict_Size(other); |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 911 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 912 | /* Do one big resize at the start, rather than |
| 913 | * incrementally resizing as we insert new keys. Expect |
| 914 | * that there will be no (or few) overlapping keys. |
| 915 | */ |
| 916 | if (dictsize == -1) |
| 917 | return -1; |
| 918 | if ((so->fill + dictsize)*3 >= (so->mask+1)*2) { |
| 919 | if (set_table_resize(so, (so->used + dictsize)*2) != 0) |
| 920 | return -1; |
| 921 | } |
| 922 | while (_PyDict_Next(other, &pos, &key, &value, &hash)) { |
| 923 | setentry an_entry; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 924 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 925 | an_entry.hash = hash; |
| 926 | an_entry.key = key; |
Raymond Hettinger | 5af9e13 | 2015-05-13 01:44:36 -0700 | [diff] [blame] | 927 | if (set_add_entry(so, &an_entry)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 928 | return -1; |
| 929 | } |
| 930 | return 0; |
| 931 | } |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 932 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 933 | it = PyObject_GetIter(other); |
| 934 | if (it == NULL) |
| 935 | return -1; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 936 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 937 | while ((key = PyIter_Next(it)) != NULL) { |
Raymond Hettinger | 5af9e13 | 2015-05-13 01:44:36 -0700 | [diff] [blame] | 938 | if (set_add_key(so, key)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 939 | Py_DECREF(it); |
| 940 | Py_DECREF(key); |
| 941 | return -1; |
| 942 | } |
| 943 | Py_DECREF(key); |
| 944 | } |
| 945 | Py_DECREF(it); |
| 946 | if (PyErr_Occurred()) |
| 947 | return -1; |
| 948 | return 0; |
Raymond Hettinger | d794666 | 2005-08-01 21:39:29 +0000 | [diff] [blame] | 949 | } |
| 950 | |
| 951 | static PyObject * |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 952 | set_update(PySetObject *so, PyObject *args) |
Raymond Hettinger | d794666 | 2005-08-01 21:39:29 +0000 | [diff] [blame] | 953 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 954 | Py_ssize_t i; |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 955 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 956 | for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) { |
| 957 | PyObject *other = PyTuple_GET_ITEM(args, i); |
Raymond Hettinger | 5af9e13 | 2015-05-13 01:44:36 -0700 | [diff] [blame] | 958 | if (set_update_internal(so, other)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 959 | return NULL; |
| 960 | } |
| 961 | Py_RETURN_NONE; |
Raymond Hettinger | a38123e | 2003-11-24 22:18:49 +0000 | [diff] [blame] | 962 | } |
| 963 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 964 | PyDoc_STRVAR(update_doc, |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 965 | "Update a set with the union of itself and others."); |
Raymond Hettinger | a38123e | 2003-11-24 22:18:49 +0000 | [diff] [blame] | 966 | |
Raymond Hettinger | 426d995 | 2014-05-18 21:40:20 +0100 | [diff] [blame] | 967 | /* XXX Todo: |
| 968 | If aligned memory allocations become available, make the |
| 969 | set object 64 byte aligned so that most of the fields |
| 970 | can be retrieved or updated in a single cache line. |
| 971 | */ |
| 972 | |
Raymond Hettinger | a38123e | 2003-11-24 22:18:49 +0000 | [diff] [blame] | 973 | static PyObject * |
| 974 | make_new_set(PyTypeObject *type, PyObject *iterable) |
| 975 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 976 | PySetObject *so = NULL; |
Raymond Hettinger | a38123e | 2003-11-24 22:18:49 +0000 | [diff] [blame] | 977 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 978 | /* create PySetObject structure */ |
Raymond Hettinger | 8f8839e | 2013-09-07 20:26:50 -0700 | [diff] [blame] | 979 | so = (PySetObject *)type->tp_alloc(type, 0); |
| 980 | if (so == NULL) |
| 981 | return NULL; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 982 | |
Raymond Hettinger | 4ea9080 | 2013-09-07 21:01:29 -0700 | [diff] [blame] | 983 | so->fill = 0; |
| 984 | so->used = 0; |
| 985 | so->mask = PySet_MINSIZE - 1; |
| 986 | so->table = so->smalltable; |
Raymond Hettinger | 4ea9080 | 2013-09-07 21:01:29 -0700 | [diff] [blame] | 987 | so->hash = -1; |
Raymond Hettinger | 1202a47 | 2015-01-18 13:12:42 -0800 | [diff] [blame] | 988 | so->finger = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 989 | so->weakreflist = NULL; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 990 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 991 | if (iterable != NULL) { |
Raymond Hettinger | 5af9e13 | 2015-05-13 01:44:36 -0700 | [diff] [blame] | 992 | if (set_update_internal(so, iterable)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 993 | Py_DECREF(so); |
| 994 | return NULL; |
| 995 | } |
| 996 | } |
Raymond Hettinger | a38123e | 2003-11-24 22:18:49 +0000 | [diff] [blame] | 997 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 998 | return (PyObject *)so; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 999 | } |
| 1000 | |
Raymond Hettinger | 7d99f09 | 2008-11-16 11:44:54 +0000 | [diff] [blame] | 1001 | static PyObject * |
| 1002 | make_new_set_basetype(PyTypeObject *type, PyObject *iterable) |
| 1003 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1004 | if (type != &PySet_Type && type != &PyFrozenSet_Type) { |
| 1005 | if (PyType_IsSubtype(type, &PySet_Type)) |
| 1006 | type = &PySet_Type; |
| 1007 | else |
| 1008 | type = &PyFrozenSet_Type; |
| 1009 | } |
| 1010 | return make_new_set(type, iterable); |
Raymond Hettinger | 7d99f09 | 2008-11-16 11:44:54 +0000 | [diff] [blame] | 1011 | } |
| 1012 | |
Raymond Hettinger | d794666 | 2005-08-01 21:39:29 +0000 | [diff] [blame] | 1013 | /* The empty frozenset is a singleton */ |
| 1014 | static PyObject *emptyfrozenset = NULL; |
| 1015 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1016 | static PyObject * |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 1017 | frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1018 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1019 | PyObject *iterable = NULL, *result; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1020 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1021 | if (type == &PyFrozenSet_Type && !_PyArg_NoKeywords("frozenset()", kwds)) |
| 1022 | return NULL; |
Georg Brandl | 02c4287 | 2005-08-26 06:42:30 +0000 | [diff] [blame] | 1023 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1024 | if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable)) |
| 1025 | return NULL; |
Raymond Hettinger | d794666 | 2005-08-01 21:39:29 +0000 | [diff] [blame] | 1026 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1027 | if (type != &PyFrozenSet_Type) |
| 1028 | return make_new_set(type, iterable); |
Raymond Hettinger | d794666 | 2005-08-01 21:39:29 +0000 | [diff] [blame] | 1029 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1030 | if (iterable != NULL) { |
| 1031 | /* frozenset(f) is idempotent */ |
| 1032 | if (PyFrozenSet_CheckExact(iterable)) { |
| 1033 | Py_INCREF(iterable); |
| 1034 | return iterable; |
| 1035 | } |
| 1036 | result = make_new_set(type, iterable); |
| 1037 | if (result == NULL || PySet_GET_SIZE(result)) |
| 1038 | return result; |
| 1039 | Py_DECREF(result); |
| 1040 | } |
| 1041 | /* The empty frozenset is a singleton */ |
| 1042 | if (emptyfrozenset == NULL) |
| 1043 | emptyfrozenset = make_new_set(type, NULL); |
| 1044 | Py_XINCREF(emptyfrozenset); |
| 1045 | return emptyfrozenset; |
Raymond Hettinger | d794666 | 2005-08-01 21:39:29 +0000 | [diff] [blame] | 1046 | } |
| 1047 | |
Antoine Pitrou | 093ce9c | 2011-12-16 11:24:27 +0100 | [diff] [blame] | 1048 | int |
| 1049 | PySet_ClearFreeList(void) |
Raymond Hettinger | d794666 | 2005-08-01 21:39:29 +0000 | [diff] [blame] | 1050 | { |
Raymond Hettinger | 8f8839e | 2013-09-07 20:26:50 -0700 | [diff] [blame] | 1051 | return 0; |
Antoine Pitrou | 093ce9c | 2011-12-16 11:24:27 +0100 | [diff] [blame] | 1052 | } |
| 1053 | |
| 1054 | void |
| 1055 | PySet_Fini(void) |
| 1056 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1057 | Py_CLEAR(emptyfrozenset); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1058 | } |
| 1059 | |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 1060 | static PyObject * |
| 1061 | set_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 1062 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1063 | if (type == &PySet_Type && !_PyArg_NoKeywords("set()", kwds)) |
| 1064 | return NULL; |
| 1065 | |
| 1066 | return make_new_set(type, NULL); |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 1067 | } |
| 1068 | |
Raymond Hettinger | 934d63e | 2005-07-31 01:33:10 +0000 | [diff] [blame] | 1069 | /* set_swap_bodies() switches the contents of any two sets by moving their |
| 1070 | internal data pointers and, if needed, copying the internal smalltables. |
| 1071 | Semantically equivalent to: |
| 1072 | |
| 1073 | t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t |
| 1074 | |
| 1075 | 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] | 1076 | Useful for operations that update in-place (by allowing an intermediate |
| 1077 | result to be swapped into one of the original inputs). |
Raymond Hettinger | 934d63e | 2005-07-31 01:33:10 +0000 | [diff] [blame] | 1078 | */ |
| 1079 | |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1080 | static void |
| 1081 | set_swap_bodies(PySetObject *a, PySetObject *b) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1082 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1083 | Py_ssize_t t; |
| 1084 | setentry *u; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1085 | setentry tab[PySet_MINSIZE]; |
Antoine Pitrou | fbb1c61 | 2010-10-23 17:37:54 +0000 | [diff] [blame] | 1086 | Py_hash_t h; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1087 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1088 | t = a->fill; a->fill = b->fill; b->fill = t; |
| 1089 | t = a->used; a->used = b->used; b->used = t; |
| 1090 | t = a->mask; a->mask = b->mask; b->mask = t; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1091 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1092 | u = a->table; |
| 1093 | if (a->table == a->smalltable) |
| 1094 | u = b->smalltable; |
| 1095 | a->table = b->table; |
| 1096 | if (b->table == b->smalltable) |
| 1097 | a->table = a->smalltable; |
| 1098 | b->table = u; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1099 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1100 | if (a->table == a->smalltable || b->table == b->smalltable) { |
| 1101 | memcpy(tab, a->smalltable, sizeof(tab)); |
| 1102 | memcpy(a->smalltable, b->smalltable, sizeof(tab)); |
| 1103 | memcpy(b->smalltable, tab, sizeof(tab)); |
| 1104 | } |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1105 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1106 | if (PyType_IsSubtype(Py_TYPE(a), &PyFrozenSet_Type) && |
| 1107 | PyType_IsSubtype(Py_TYPE(b), &PyFrozenSet_Type)) { |
| 1108 | h = a->hash; a->hash = b->hash; b->hash = h; |
| 1109 | } else { |
| 1110 | a->hash = -1; |
| 1111 | b->hash = -1; |
| 1112 | } |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1113 | } |
| 1114 | |
Raymond Hettinger | 8f5cdaa | 2003-12-13 11:26:12 +0000 | [diff] [blame] | 1115 | static PyObject * |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1116 | set_copy(PySetObject *so) |
| 1117 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1118 | return make_new_set_basetype(Py_TYPE(so), (PyObject *)so); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1119 | } |
| 1120 | |
Raymond Hettinger | 49ba4c3 | 2003-11-23 02:49:05 +0000 | [diff] [blame] | 1121 | static PyObject * |
| 1122 | frozenset_copy(PySetObject *so) |
| 1123 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1124 | if (PyFrozenSet_CheckExact(so)) { |
| 1125 | Py_INCREF(so); |
| 1126 | return (PyObject *)so; |
| 1127 | } |
| 1128 | return set_copy(so); |
Raymond Hettinger | 49ba4c3 | 2003-11-23 02:49:05 +0000 | [diff] [blame] | 1129 | } |
| 1130 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1131 | PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set."); |
| 1132 | |
| 1133 | static PyObject * |
Raymond Hettinger | c991db2 | 2005-08-11 07:58:45 +0000 | [diff] [blame] | 1134 | set_clear(PySetObject *so) |
| 1135 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1136 | set_clear_internal(so); |
| 1137 | Py_RETURN_NONE; |
Raymond Hettinger | c991db2 | 2005-08-11 07:58:45 +0000 | [diff] [blame] | 1138 | } |
| 1139 | |
| 1140 | PyDoc_STRVAR(clear_doc, "Remove all elements from this set."); |
| 1141 | |
| 1142 | static PyObject * |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 1143 | set_union(PySetObject *so, PyObject *args) |
Raymond Hettinger | f5f41bf | 2003-11-24 02:57:33 +0000 | [diff] [blame] | 1144 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1145 | PySetObject *result; |
| 1146 | PyObject *other; |
| 1147 | Py_ssize_t i; |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 1148 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1149 | result = (PySetObject *)set_copy(so); |
| 1150 | if (result == NULL) |
| 1151 | return NULL; |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 1152 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1153 | for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) { |
| 1154 | other = PyTuple_GET_ITEM(args, i); |
| 1155 | if ((PyObject *)so == other) |
| 1156 | continue; |
Raymond Hettinger | 5af9e13 | 2015-05-13 01:44:36 -0700 | [diff] [blame] | 1157 | if (set_update_internal(result, other)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1158 | Py_DECREF(result); |
| 1159 | return NULL; |
| 1160 | } |
| 1161 | } |
| 1162 | return (PyObject *)result; |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 1163 | } |
| 1164 | |
| 1165 | PyDoc_STRVAR(union_doc, |
| 1166 | "Return the union of sets as a new set.\n\ |
| 1167 | \n\ |
| 1168 | (i.e. all elements that are in either set.)"); |
| 1169 | |
| 1170 | static PyObject * |
| 1171 | set_or(PySetObject *so, PyObject *other) |
| 1172 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1173 | PySetObject *result; |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 1174 | |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 1175 | if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) |
| 1176 | Py_RETURN_NOTIMPLEMENTED; |
Raymond Hettinger | f5f41bf | 2003-11-24 02:57:33 +0000 | [diff] [blame] | 1177 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1178 | result = (PySetObject *)set_copy(so); |
| 1179 | if (result == NULL) |
| 1180 | return NULL; |
| 1181 | if ((PyObject *)so == other) |
| 1182 | return (PyObject *)result; |
Raymond Hettinger | 5af9e13 | 2015-05-13 01:44:36 -0700 | [diff] [blame] | 1183 | if (set_update_internal(result, other)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1184 | Py_DECREF(result); |
| 1185 | return NULL; |
| 1186 | } |
| 1187 | return (PyObject *)result; |
Raymond Hettinger | f5f41bf | 2003-11-24 02:57:33 +0000 | [diff] [blame] | 1188 | } |
| 1189 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1190 | static PyObject * |
| 1191 | set_ior(PySetObject *so, PyObject *other) |
| 1192 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 1193 | if (!PyAnySet_Check(other)) |
| 1194 | Py_RETURN_NOTIMPLEMENTED; |
| 1195 | |
Raymond Hettinger | 5af9e13 | 2015-05-13 01:44:36 -0700 | [diff] [blame] | 1196 | if (set_update_internal(so, other)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1197 | return NULL; |
| 1198 | Py_INCREF(so); |
| 1199 | return (PyObject *)so; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1200 | } |
| 1201 | |
| 1202 | static PyObject * |
| 1203 | set_intersection(PySetObject *so, PyObject *other) |
| 1204 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1205 | PySetObject *result; |
| 1206 | PyObject *key, *it, *tmp; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1207 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1208 | if ((PyObject *)so == other) |
| 1209 | return set_copy(so); |
Raymond Hettinger | c991db2 | 2005-08-11 07:58:45 +0000 | [diff] [blame] | 1210 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1211 | result = (PySetObject *)make_new_set_basetype(Py_TYPE(so), NULL); |
| 1212 | if (result == NULL) |
| 1213 | return NULL; |
Raymond Hettinger | f5f41bf | 2003-11-24 02:57:33 +0000 | [diff] [blame] | 1214 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1215 | if (PyAnySet_Check(other)) { |
| 1216 | Py_ssize_t pos = 0; |
| 1217 | setentry *entry; |
Raymond Hettinger | b02c35e | 2005-08-12 20:48:39 +0000 | [diff] [blame] | 1218 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1219 | if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) { |
| 1220 | tmp = (PyObject *)so; |
| 1221 | so = (PySetObject *)other; |
| 1222 | other = tmp; |
| 1223 | } |
Raymond Hettinger | b02c35e | 2005-08-12 20:48:39 +0000 | [diff] [blame] | 1224 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1225 | while (set_next((PySetObject *)other, &pos, &entry)) { |
| 1226 | int rv = set_contains_entry(so, entry); |
| 1227 | if (rv == -1) { |
| 1228 | Py_DECREF(result); |
| 1229 | return NULL; |
| 1230 | } |
| 1231 | if (rv) { |
Raymond Hettinger | 5af9e13 | 2015-05-13 01:44:36 -0700 | [diff] [blame] | 1232 | if (set_add_entry(result, entry)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1233 | Py_DECREF(result); |
| 1234 | return NULL; |
| 1235 | } |
| 1236 | } |
| 1237 | } |
| 1238 | return (PyObject *)result; |
| 1239 | } |
Raymond Hettinger | f5f41bf | 2003-11-24 02:57:33 +0000 | [diff] [blame] | 1240 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1241 | it = PyObject_GetIter(other); |
| 1242 | if (it == NULL) { |
| 1243 | Py_DECREF(result); |
| 1244 | return NULL; |
| 1245 | } |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1246 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1247 | while ((key = PyIter_Next(it)) != NULL) { |
| 1248 | int rv; |
| 1249 | setentry entry; |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 1250 | Py_hash_t hash = PyObject_Hash(key); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1251 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1252 | if (hash == -1) { |
| 1253 | Py_DECREF(it); |
| 1254 | Py_DECREF(result); |
| 1255 | Py_DECREF(key); |
| 1256 | return NULL; |
| 1257 | } |
| 1258 | entry.hash = hash; |
| 1259 | entry.key = key; |
| 1260 | rv = set_contains_entry(so, &entry); |
| 1261 | if (rv == -1) { |
| 1262 | Py_DECREF(it); |
| 1263 | Py_DECREF(result); |
| 1264 | Py_DECREF(key); |
| 1265 | return NULL; |
| 1266 | } |
| 1267 | if (rv) { |
Raymond Hettinger | 5af9e13 | 2015-05-13 01:44:36 -0700 | [diff] [blame] | 1268 | if (set_add_entry(result, &entry)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1269 | Py_DECREF(it); |
| 1270 | Py_DECREF(result); |
| 1271 | Py_DECREF(key); |
| 1272 | return NULL; |
| 1273 | } |
| 1274 | } |
| 1275 | Py_DECREF(key); |
| 1276 | } |
| 1277 | Py_DECREF(it); |
| 1278 | if (PyErr_Occurred()) { |
| 1279 | Py_DECREF(result); |
| 1280 | return NULL; |
| 1281 | } |
| 1282 | return (PyObject *)result; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1283 | } |
| 1284 | |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 1285 | static PyObject * |
| 1286 | set_intersection_multi(PySetObject *so, PyObject *args) |
| 1287 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1288 | Py_ssize_t i; |
| 1289 | PyObject *result = (PyObject *)so; |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 1290 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1291 | if (PyTuple_GET_SIZE(args) == 0) |
| 1292 | return set_copy(so); |
Amaury Forgeot d'Arc | fdfe62d | 2008-06-17 20:36:03 +0000 | [diff] [blame] | 1293 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1294 | Py_INCREF(so); |
| 1295 | for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) { |
| 1296 | PyObject *other = PyTuple_GET_ITEM(args, i); |
| 1297 | PyObject *newresult = set_intersection((PySetObject *)result, other); |
| 1298 | if (newresult == NULL) { |
| 1299 | Py_DECREF(result); |
| 1300 | return NULL; |
| 1301 | } |
| 1302 | Py_DECREF(result); |
| 1303 | result = newresult; |
| 1304 | } |
| 1305 | return result; |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 1306 | } |
| 1307 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1308 | PyDoc_STRVAR(intersection_doc, |
| 1309 | "Return the intersection of two sets as a new set.\n\ |
| 1310 | \n\ |
| 1311 | (i.e. all elements that are in both sets.)"); |
| 1312 | |
| 1313 | static PyObject * |
| 1314 | set_intersection_update(PySetObject *so, PyObject *other) |
| 1315 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1316 | PyObject *tmp; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1317 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1318 | tmp = set_intersection(so, other); |
| 1319 | if (tmp == NULL) |
| 1320 | return NULL; |
| 1321 | set_swap_bodies(so, (PySetObject *)tmp); |
| 1322 | Py_DECREF(tmp); |
| 1323 | Py_RETURN_NONE; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1324 | } |
| 1325 | |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 1326 | static PyObject * |
| 1327 | set_intersection_update_multi(PySetObject *so, PyObject *args) |
| 1328 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1329 | PyObject *tmp; |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 1330 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1331 | tmp = set_intersection_multi(so, args); |
| 1332 | if (tmp == NULL) |
| 1333 | return NULL; |
| 1334 | set_swap_bodies(so, (PySetObject *)tmp); |
| 1335 | Py_DECREF(tmp); |
| 1336 | Py_RETURN_NONE; |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 1337 | } |
| 1338 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1339 | PyDoc_STRVAR(intersection_update_doc, |
| 1340 | "Update a set with the intersection of itself and another."); |
| 1341 | |
| 1342 | static PyObject * |
| 1343 | set_and(PySetObject *so, PyObject *other) |
| 1344 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 1345 | if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) |
| 1346 | Py_RETURN_NOTIMPLEMENTED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1347 | return set_intersection(so, other); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1348 | } |
| 1349 | |
| 1350 | static PyObject * |
| 1351 | set_iand(PySetObject *so, PyObject *other) |
| 1352 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1353 | PyObject *result; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1354 | |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 1355 | if (!PyAnySet_Check(other)) |
| 1356 | Py_RETURN_NOTIMPLEMENTED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1357 | result = set_intersection_update(so, other); |
| 1358 | if (result == NULL) |
| 1359 | return NULL; |
| 1360 | Py_DECREF(result); |
| 1361 | Py_INCREF(so); |
| 1362 | return (PyObject *)so; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1363 | } |
| 1364 | |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +0000 | [diff] [blame] | 1365 | static PyObject * |
| 1366 | set_isdisjoint(PySetObject *so, PyObject *other) |
| 1367 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1368 | PyObject *key, *it, *tmp; |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +0000 | [diff] [blame] | 1369 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1370 | if ((PyObject *)so == other) { |
| 1371 | if (PySet_GET_SIZE(so) == 0) |
| 1372 | Py_RETURN_TRUE; |
| 1373 | else |
| 1374 | Py_RETURN_FALSE; |
| 1375 | } |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +0000 | [diff] [blame] | 1376 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1377 | if (PyAnySet_CheckExact(other)) { |
| 1378 | Py_ssize_t pos = 0; |
| 1379 | setentry *entry; |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +0000 | [diff] [blame] | 1380 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1381 | if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) { |
| 1382 | tmp = (PyObject *)so; |
| 1383 | so = (PySetObject *)other; |
| 1384 | other = tmp; |
| 1385 | } |
| 1386 | while (set_next((PySetObject *)other, &pos, &entry)) { |
| 1387 | int rv = set_contains_entry(so, entry); |
| 1388 | if (rv == -1) |
| 1389 | return NULL; |
| 1390 | if (rv) |
| 1391 | Py_RETURN_FALSE; |
| 1392 | } |
| 1393 | Py_RETURN_TRUE; |
| 1394 | } |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +0000 | [diff] [blame] | 1395 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1396 | it = PyObject_GetIter(other); |
| 1397 | if (it == NULL) |
| 1398 | return NULL; |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +0000 | [diff] [blame] | 1399 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1400 | while ((key = PyIter_Next(it)) != NULL) { |
| 1401 | int rv; |
| 1402 | setentry entry; |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 1403 | Py_hash_t hash = PyObject_Hash(key); |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +0000 | [diff] [blame] | 1404 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1405 | if (hash == -1) { |
| 1406 | Py_DECREF(key); |
| 1407 | Py_DECREF(it); |
| 1408 | return NULL; |
| 1409 | } |
| 1410 | entry.hash = hash; |
| 1411 | entry.key = key; |
| 1412 | rv = set_contains_entry(so, &entry); |
| 1413 | Py_DECREF(key); |
| 1414 | if (rv == -1) { |
| 1415 | Py_DECREF(it); |
| 1416 | return NULL; |
| 1417 | } |
| 1418 | if (rv) { |
| 1419 | Py_DECREF(it); |
| 1420 | Py_RETURN_FALSE; |
| 1421 | } |
| 1422 | } |
| 1423 | Py_DECREF(it); |
| 1424 | if (PyErr_Occurred()) |
| 1425 | return NULL; |
| 1426 | Py_RETURN_TRUE; |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +0000 | [diff] [blame] | 1427 | } |
| 1428 | |
| 1429 | PyDoc_STRVAR(isdisjoint_doc, |
| 1430 | "Return True if two sets have a null intersection."); |
| 1431 | |
Neal Norwitz | 6576bd8 | 2005-11-13 18:41:28 +0000 | [diff] [blame] | 1432 | static int |
Raymond Hettinger | c991db2 | 2005-08-11 07:58:45 +0000 | [diff] [blame] | 1433 | set_difference_update_internal(PySetObject *so, PyObject *other) |
| 1434 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1435 | if ((PyObject *)so == other) |
| 1436 | return set_clear_internal(so); |
Raymond Hettinger | c991db2 | 2005-08-11 07:58:45 +0000 | [diff] [blame] | 1437 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1438 | if (PyAnySet_Check(other)) { |
| 1439 | setentry *entry; |
| 1440 | Py_ssize_t pos = 0; |
Raymond Hettinger | c991db2 | 2005-08-11 07:58:45 +0000 | [diff] [blame] | 1441 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1442 | while (set_next((PySetObject *)other, &pos, &entry)) |
| 1443 | if (set_discard_entry(so, entry) == -1) |
| 1444 | return -1; |
| 1445 | } else { |
| 1446 | PyObject *key, *it; |
| 1447 | it = PyObject_GetIter(other); |
| 1448 | if (it == NULL) |
| 1449 | return -1; |
| 1450 | |
| 1451 | while ((key = PyIter_Next(it)) != NULL) { |
| 1452 | if (set_discard_key(so, key) == -1) { |
| 1453 | Py_DECREF(it); |
| 1454 | Py_DECREF(key); |
| 1455 | return -1; |
| 1456 | } |
| 1457 | Py_DECREF(key); |
| 1458 | } |
| 1459 | Py_DECREF(it); |
| 1460 | if (PyErr_Occurred()) |
| 1461 | return -1; |
| 1462 | } |
| 1463 | /* If more than 1/5 are dummies, then resize them away. */ |
| 1464 | if ((so->fill - so->used) * 5 < so->mask) |
| 1465 | return 0; |
| 1466 | 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] | 1467 | } |
| 1468 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1469 | static PyObject * |
Amaury Forgeot d'Arc | fdfe62d | 2008-06-17 20:36:03 +0000 | [diff] [blame] | 1470 | set_difference_update(PySetObject *so, PyObject *args) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1471 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1472 | Py_ssize_t i; |
Amaury Forgeot d'Arc | fdfe62d | 2008-06-17 20:36:03 +0000 | [diff] [blame] | 1473 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1474 | for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) { |
| 1475 | PyObject *other = PyTuple_GET_ITEM(args, i); |
Raymond Hettinger | 5af9e13 | 2015-05-13 01:44:36 -0700 | [diff] [blame] | 1476 | if (set_difference_update_internal(so, other)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1477 | return NULL; |
| 1478 | } |
| 1479 | Py_RETURN_NONE; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1480 | } |
| 1481 | |
| 1482 | PyDoc_STRVAR(difference_update_doc, |
| 1483 | "Remove all elements of another set from this set."); |
| 1484 | |
| 1485 | static PyObject * |
Antoine Pitrou | 715f3cd | 2010-11-30 22:23:20 +0000 | [diff] [blame] | 1486 | set_copy_and_difference(PySetObject *so, PyObject *other) |
| 1487 | { |
| 1488 | PyObject *result; |
| 1489 | |
| 1490 | result = set_copy(so); |
| 1491 | if (result == NULL) |
| 1492 | return NULL; |
| 1493 | if (set_difference_update_internal((PySetObject *) result, other) != -1) |
| 1494 | return result; |
| 1495 | Py_DECREF(result); |
| 1496 | return NULL; |
| 1497 | } |
| 1498 | |
| 1499 | static PyObject * |
Raymond Hettinger | fb4e33a | 2003-12-15 13:23:55 +0000 | [diff] [blame] | 1500 | set_difference(PySetObject *so, PyObject *other) |
| 1501 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1502 | PyObject *result; |
| 1503 | setentry *entry; |
| 1504 | Py_ssize_t pos = 0; |
Raymond Hettinger | fb4e33a | 2003-12-15 13:23:55 +0000 | [diff] [blame] | 1505 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1506 | if (!PyAnySet_Check(other) && !PyDict_CheckExact(other)) { |
Antoine Pitrou | 715f3cd | 2010-11-30 22:23:20 +0000 | [diff] [blame] | 1507 | return set_copy_and_difference(so, other); |
| 1508 | } |
| 1509 | |
| 1510 | /* If len(so) much more than len(other), it's more efficient to simply copy |
| 1511 | * so and then iterate other looking for common elements. */ |
| 1512 | if ((PySet_GET_SIZE(so) >> 2) > PyObject_Size(other)) { |
| 1513 | return set_copy_and_difference(so, other); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1514 | } |
Raymond Hettinger | fb4e33a | 2003-12-15 13:23:55 +0000 | [diff] [blame] | 1515 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1516 | result = make_new_set_basetype(Py_TYPE(so), NULL); |
| 1517 | if (result == NULL) |
| 1518 | return NULL; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1519 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1520 | if (PyDict_CheckExact(other)) { |
| 1521 | while (set_next(so, &pos, &entry)) { |
| 1522 | setentry entrycopy; |
Serhiy Storchaka | fa494fd | 2015-05-30 17:45:22 +0300 | [diff] [blame] | 1523 | int rv; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1524 | entrycopy.hash = entry->hash; |
| 1525 | entrycopy.key = entry->key; |
Serhiy Storchaka | fa494fd | 2015-05-30 17:45:22 +0300 | [diff] [blame] | 1526 | rv = _PyDict_Contains(other, entry->key, entry->hash); |
| 1527 | if (rv < 0) { |
| 1528 | Py_DECREF(result); |
| 1529 | return NULL; |
| 1530 | } |
| 1531 | if (!rv) { |
Raymond Hettinger | 5af9e13 | 2015-05-13 01:44:36 -0700 | [diff] [blame] | 1532 | if (set_add_entry((PySetObject *)result, &entrycopy)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1533 | Py_DECREF(result); |
| 1534 | return NULL; |
| 1535 | } |
| 1536 | } |
| 1537 | } |
| 1538 | return result; |
| 1539 | } |
| 1540 | |
Antoine Pitrou | 715f3cd | 2010-11-30 22:23:20 +0000 | [diff] [blame] | 1541 | /* Iterate over so, checking for common elements in other. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1542 | while (set_next(so, &pos, &entry)) { |
| 1543 | int rv = set_contains_entry((PySetObject *)other, entry); |
| 1544 | if (rv == -1) { |
| 1545 | Py_DECREF(result); |
| 1546 | return NULL; |
| 1547 | } |
| 1548 | if (!rv) { |
Raymond Hettinger | 5af9e13 | 2015-05-13 01:44:36 -0700 | [diff] [blame] | 1549 | if (set_add_entry((PySetObject *)result, entry)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1550 | Py_DECREF(result); |
| 1551 | return NULL; |
| 1552 | } |
| 1553 | } |
| 1554 | } |
| 1555 | return result; |
Raymond Hettinger | fb4e33a | 2003-12-15 13:23:55 +0000 | [diff] [blame] | 1556 | } |
| 1557 | |
Amaury Forgeot d'Arc | fdfe62d | 2008-06-17 20:36:03 +0000 | [diff] [blame] | 1558 | static PyObject * |
| 1559 | set_difference_multi(PySetObject *so, PyObject *args) |
| 1560 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1561 | Py_ssize_t i; |
| 1562 | PyObject *result, *other; |
Amaury Forgeot d'Arc | fdfe62d | 2008-06-17 20:36:03 +0000 | [diff] [blame] | 1563 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1564 | if (PyTuple_GET_SIZE(args) == 0) |
| 1565 | return set_copy(so); |
Amaury Forgeot d'Arc | fdfe62d | 2008-06-17 20:36:03 +0000 | [diff] [blame] | 1566 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1567 | other = PyTuple_GET_ITEM(args, 0); |
| 1568 | result = set_difference(so, other); |
| 1569 | if (result == NULL) |
| 1570 | return NULL; |
Amaury Forgeot d'Arc | fdfe62d | 2008-06-17 20:36:03 +0000 | [diff] [blame] | 1571 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1572 | for (i=1 ; i<PyTuple_GET_SIZE(args) ; i++) { |
| 1573 | other = PyTuple_GET_ITEM(args, i); |
Raymond Hettinger | 5af9e13 | 2015-05-13 01:44:36 -0700 | [diff] [blame] | 1574 | if (set_difference_update_internal((PySetObject *)result, other)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1575 | Py_DECREF(result); |
| 1576 | return NULL; |
| 1577 | } |
| 1578 | } |
| 1579 | return result; |
Amaury Forgeot d'Arc | fdfe62d | 2008-06-17 20:36:03 +0000 | [diff] [blame] | 1580 | } |
| 1581 | |
Raymond Hettinger | fb4e33a | 2003-12-15 13:23:55 +0000 | [diff] [blame] | 1582 | PyDoc_STRVAR(difference_doc, |
Amaury Forgeot d'Arc | fdfe62d | 2008-06-17 20:36:03 +0000 | [diff] [blame] | 1583 | "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] | 1584 | \n\ |
Amaury Forgeot d'Arc | fdfe62d | 2008-06-17 20:36:03 +0000 | [diff] [blame] | 1585 | (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] | 1586 | static PyObject * |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1587 | set_sub(PySetObject *so, PyObject *other) |
| 1588 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 1589 | if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) |
| 1590 | Py_RETURN_NOTIMPLEMENTED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1591 | return set_difference(so, other); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1592 | } |
| 1593 | |
| 1594 | static PyObject * |
| 1595 | set_isub(PySetObject *so, PyObject *other) |
| 1596 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 1597 | if (!PyAnySet_Check(other)) |
| 1598 | Py_RETURN_NOTIMPLEMENTED; |
Raymond Hettinger | 5af9e13 | 2015-05-13 01:44:36 -0700 | [diff] [blame] | 1599 | if (set_difference_update_internal(so, other)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1600 | return NULL; |
| 1601 | Py_INCREF(so); |
| 1602 | return (PyObject *)so; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1603 | } |
| 1604 | |
| 1605 | static PyObject * |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1606 | set_symmetric_difference_update(PySetObject *so, PyObject *other) |
| 1607 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1608 | PySetObject *otherset; |
| 1609 | PyObject *key; |
| 1610 | Py_ssize_t pos = 0; |
| 1611 | setentry *entry; |
Raymond Hettinger | c991db2 | 2005-08-11 07:58:45 +0000 | [diff] [blame] | 1612 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1613 | if ((PyObject *)so == other) |
| 1614 | return set_clear(so); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1615 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1616 | if (PyDict_CheckExact(other)) { |
| 1617 | PyObject *value; |
| 1618 | int rv; |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 1619 | Py_hash_t hash; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1620 | while (_PyDict_Next(other, &pos, &key, &value, &hash)) { |
| 1621 | setentry an_entry; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1622 | |
Raymond Hettinger | faf7b7f | 2010-09-03 10:00:50 +0000 | [diff] [blame] | 1623 | Py_INCREF(key); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1624 | an_entry.hash = hash; |
| 1625 | an_entry.key = key; |
Raymond Hettinger | faf7b7f | 2010-09-03 10:00:50 +0000 | [diff] [blame] | 1626 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1627 | rv = set_discard_entry(so, &an_entry); |
Georg Brandl | 2d44449 | 2010-09-03 10:52:55 +0000 | [diff] [blame] | 1628 | if (rv == -1) { |
Raymond Hettinger | faf7b7f | 2010-09-03 10:00:50 +0000 | [diff] [blame] | 1629 | Py_DECREF(key); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1630 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1631 | } |
Raymond Hettinger | faf7b7f | 2010-09-03 10:00:50 +0000 | [diff] [blame] | 1632 | if (rv == DISCARD_NOTFOUND) { |
Raymond Hettinger | 5af9e13 | 2015-05-13 01:44:36 -0700 | [diff] [blame] | 1633 | if (set_add_entry(so, &an_entry)) { |
Raymond Hettinger | faf7b7f | 2010-09-03 10:00:50 +0000 | [diff] [blame] | 1634 | Py_DECREF(key); |
| 1635 | return NULL; |
| 1636 | } |
| 1637 | } |
Georg Brandl | 2d44449 | 2010-09-03 10:52:55 +0000 | [diff] [blame] | 1638 | Py_DECREF(key); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1639 | } |
| 1640 | Py_RETURN_NONE; |
| 1641 | } |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1642 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1643 | if (PyAnySet_Check(other)) { |
| 1644 | Py_INCREF(other); |
| 1645 | otherset = (PySetObject *)other; |
| 1646 | } else { |
| 1647 | otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other); |
| 1648 | if (otherset == NULL) |
| 1649 | return NULL; |
| 1650 | } |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1651 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1652 | while (set_next(otherset, &pos, &entry)) { |
| 1653 | int rv = set_discard_entry(so, entry); |
| 1654 | if (rv == -1) { |
| 1655 | Py_DECREF(otherset); |
| 1656 | return NULL; |
| 1657 | } |
| 1658 | if (rv == DISCARD_NOTFOUND) { |
Raymond Hettinger | 5af9e13 | 2015-05-13 01:44:36 -0700 | [diff] [blame] | 1659 | if (set_add_entry(so, entry)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1660 | Py_DECREF(otherset); |
| 1661 | return NULL; |
| 1662 | } |
| 1663 | } |
| 1664 | } |
| 1665 | Py_DECREF(otherset); |
| 1666 | Py_RETURN_NONE; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1667 | } |
| 1668 | |
| 1669 | PyDoc_STRVAR(symmetric_difference_update_doc, |
| 1670 | "Update a set with the symmetric difference of itself and another."); |
| 1671 | |
| 1672 | static PyObject * |
Raymond Hettinger | f5f41bf | 2003-11-24 02:57:33 +0000 | [diff] [blame] | 1673 | set_symmetric_difference(PySetObject *so, PyObject *other) |
| 1674 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1675 | PyObject *rv; |
| 1676 | PySetObject *otherset; |
Raymond Hettinger | f5f41bf | 2003-11-24 02:57:33 +0000 | [diff] [blame] | 1677 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1678 | otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other); |
| 1679 | if (otherset == NULL) |
| 1680 | return NULL; |
| 1681 | rv = set_symmetric_difference_update(otherset, (PyObject *)so); |
| 1682 | if (rv == NULL) |
| 1683 | return NULL; |
| 1684 | Py_DECREF(rv); |
| 1685 | return (PyObject *)otherset; |
Raymond Hettinger | f5f41bf | 2003-11-24 02:57:33 +0000 | [diff] [blame] | 1686 | } |
| 1687 | |
| 1688 | PyDoc_STRVAR(symmetric_difference_doc, |
| 1689 | "Return the symmetric difference of two sets as a new set.\n\ |
| 1690 | \n\ |
| 1691 | (i.e. all elements that are in exactly one of the sets.)"); |
| 1692 | |
| 1693 | static PyObject * |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1694 | set_xor(PySetObject *so, PyObject *other) |
| 1695 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 1696 | if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) |
| 1697 | Py_RETURN_NOTIMPLEMENTED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1698 | return set_symmetric_difference(so, other); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1699 | } |
| 1700 | |
| 1701 | static PyObject * |
| 1702 | set_ixor(PySetObject *so, PyObject *other) |
| 1703 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1704 | PyObject *result; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1705 | |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 1706 | if (!PyAnySet_Check(other)) |
| 1707 | Py_RETURN_NOTIMPLEMENTED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1708 | result = set_symmetric_difference_update(so, other); |
| 1709 | if (result == NULL) |
| 1710 | return NULL; |
| 1711 | Py_DECREF(result); |
| 1712 | Py_INCREF(so); |
| 1713 | return (PyObject *)so; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1714 | } |
| 1715 | |
| 1716 | static PyObject * |
| 1717 | set_issubset(PySetObject *so, PyObject *other) |
| 1718 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1719 | setentry *entry; |
| 1720 | Py_ssize_t pos = 0; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1721 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1722 | if (!PyAnySet_Check(other)) { |
| 1723 | PyObject *tmp, *result; |
| 1724 | tmp = make_new_set(&PySet_Type, other); |
| 1725 | if (tmp == NULL) |
| 1726 | return NULL; |
| 1727 | result = set_issubset(so, tmp); |
| 1728 | Py_DECREF(tmp); |
| 1729 | return result; |
| 1730 | } |
| 1731 | if (PySet_GET_SIZE(so) > PySet_GET_SIZE(other)) |
| 1732 | Py_RETURN_FALSE; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1733 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1734 | while (set_next(so, &pos, &entry)) { |
| 1735 | int rv = set_contains_entry((PySetObject *)other, entry); |
| 1736 | if (rv == -1) |
| 1737 | return NULL; |
| 1738 | if (!rv) |
| 1739 | Py_RETURN_FALSE; |
| 1740 | } |
| 1741 | Py_RETURN_TRUE; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1742 | } |
| 1743 | |
| 1744 | PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set."); |
| 1745 | |
| 1746 | static PyObject * |
| 1747 | set_issuperset(PySetObject *so, PyObject *other) |
| 1748 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1749 | PyObject *tmp, *result; |
Raymond Hettinger | 3fbec70 | 2003-11-21 07:56:36 +0000 | [diff] [blame] | 1750 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1751 | if (!PyAnySet_Check(other)) { |
| 1752 | tmp = make_new_set(&PySet_Type, other); |
| 1753 | if (tmp == NULL) |
| 1754 | return NULL; |
| 1755 | result = set_issuperset(so, tmp); |
| 1756 | Py_DECREF(tmp); |
| 1757 | return result; |
| 1758 | } |
| 1759 | return set_issubset((PySetObject *)other, (PyObject *)so); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1760 | } |
| 1761 | |
| 1762 | PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set."); |
| 1763 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1764 | static PyObject * |
| 1765 | set_richcompare(PySetObject *v, PyObject *w, int op) |
| 1766 | { |
Serhiy Storchaka | fa494fd | 2015-05-30 17:45:22 +0300 | [diff] [blame] | 1767 | PyObject *r1; |
| 1768 | int r2; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1769 | |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 1770 | if(!PyAnySet_Check(w)) |
| 1771 | Py_RETURN_NOTIMPLEMENTED; |
| 1772 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1773 | switch (op) { |
| 1774 | case Py_EQ: |
| 1775 | if (PySet_GET_SIZE(v) != PySet_GET_SIZE(w)) |
| 1776 | Py_RETURN_FALSE; |
| 1777 | if (v->hash != -1 && |
| 1778 | ((PySetObject *)w)->hash != -1 && |
| 1779 | v->hash != ((PySetObject *)w)->hash) |
| 1780 | Py_RETURN_FALSE; |
| 1781 | return set_issubset(v, w); |
| 1782 | case Py_NE: |
| 1783 | r1 = set_richcompare(v, w, Py_EQ); |
| 1784 | if (r1 == NULL) |
| 1785 | return NULL; |
Serhiy Storchaka | fa494fd | 2015-05-30 17:45:22 +0300 | [diff] [blame] | 1786 | r2 = PyObject_IsTrue(r1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1787 | Py_DECREF(r1); |
Serhiy Storchaka | fa494fd | 2015-05-30 17:45:22 +0300 | [diff] [blame] | 1788 | if (r2 < 0) |
| 1789 | return NULL; |
| 1790 | return PyBool_FromLong(!r2); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1791 | case Py_LE: |
| 1792 | return set_issubset(v, w); |
| 1793 | case Py_GE: |
| 1794 | return set_issuperset(v, w); |
| 1795 | case Py_LT: |
| 1796 | if (PySet_GET_SIZE(v) >= PySet_GET_SIZE(w)) |
| 1797 | Py_RETURN_FALSE; |
| 1798 | return set_issubset(v, w); |
| 1799 | case Py_GT: |
| 1800 | if (PySet_GET_SIZE(v) <= PySet_GET_SIZE(w)) |
| 1801 | Py_RETURN_FALSE; |
| 1802 | return set_issuperset(v, w); |
| 1803 | } |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 1804 | Py_RETURN_NOTIMPLEMENTED; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1805 | } |
| 1806 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1807 | static PyObject * |
Raymond Hettinger | 06d8cf8 | 2005-07-31 15:36:06 +0000 | [diff] [blame] | 1808 | set_add(PySetObject *so, PyObject *key) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1809 | { |
Raymond Hettinger | 5af9e13 | 2015-05-13 01:44:36 -0700 | [diff] [blame] | 1810 | if (set_add_key(so, key)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1811 | return NULL; |
| 1812 | Py_RETURN_NONE; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1813 | } |
| 1814 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1815 | PyDoc_STRVAR(add_doc, |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1816 | "Add an element to a set.\n\ |
| 1817 | \n\ |
| 1818 | This has no effect if the element is already present."); |
| 1819 | |
Raymond Hettinger | ce8185e | 2005-08-13 09:28:48 +0000 | [diff] [blame] | 1820 | static int |
| 1821 | set_contains(PySetObject *so, PyObject *key) |
| 1822 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1823 | PyObject *tmpkey; |
| 1824 | int rv; |
Raymond Hettinger | ce8185e | 2005-08-13 09:28:48 +0000 | [diff] [blame] | 1825 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1826 | rv = set_contains_key(so, key); |
| 1827 | if (rv == -1) { |
| 1828 | if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError)) |
| 1829 | return -1; |
| 1830 | PyErr_Clear(); |
Raymond Hettinger | 38bf2cc | 2010-08-06 09:52:17 +0000 | [diff] [blame] | 1831 | tmpkey = make_new_set(&PyFrozenSet_Type, key); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1832 | if (tmpkey == NULL) |
| 1833 | return -1; |
Petri Lehtinen | 5acc27e | 2011-10-30 13:56:41 +0200 | [diff] [blame] | 1834 | rv = set_contains_key(so, tmpkey); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1835 | Py_DECREF(tmpkey); |
| 1836 | } |
| 1837 | return rv; |
Raymond Hettinger | ce8185e | 2005-08-13 09:28:48 +0000 | [diff] [blame] | 1838 | } |
| 1839 | |
| 1840 | static PyObject * |
| 1841 | set_direct_contains(PySetObject *so, PyObject *key) |
| 1842 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1843 | long result; |
Raymond Hettinger | ce8185e | 2005-08-13 09:28:48 +0000 | [diff] [blame] | 1844 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1845 | result = set_contains(so, key); |
| 1846 | if (result == -1) |
| 1847 | return NULL; |
| 1848 | return PyBool_FromLong(result); |
Raymond Hettinger | ce8185e | 2005-08-13 09:28:48 +0000 | [diff] [blame] | 1849 | } |
| 1850 | |
| 1851 | PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x."); |
| 1852 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1853 | static PyObject * |
Raymond Hettinger | 06d8cf8 | 2005-07-31 15:36:06 +0000 | [diff] [blame] | 1854 | set_remove(PySetObject *so, PyObject *key) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1855 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1856 | PyObject *tmpkey; |
| 1857 | int rv; |
Raymond Hettinger | bfd334a | 2003-11-22 03:55:23 +0000 | [diff] [blame] | 1858 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1859 | rv = set_discard_key(so, key); |
| 1860 | if (rv == -1) { |
| 1861 | if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError)) |
| 1862 | return NULL; |
| 1863 | PyErr_Clear(); |
Raymond Hettinger | 38bf2cc | 2010-08-06 09:52:17 +0000 | [diff] [blame] | 1864 | tmpkey = make_new_set(&PyFrozenSet_Type, key); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1865 | if (tmpkey == NULL) |
| 1866 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1867 | rv = set_discard_key(so, tmpkey); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1868 | Py_DECREF(tmpkey); |
| 1869 | if (rv == -1) |
| 1870 | return NULL; |
| 1871 | } |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 1872 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1873 | if (rv == DISCARD_NOTFOUND) { |
Raymond Hettinger | 69492da | 2013-09-02 15:59:26 -0700 | [diff] [blame] | 1874 | _PyErr_SetKeyError(key); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1875 | return NULL; |
| 1876 | } |
| 1877 | Py_RETURN_NONE; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1878 | } |
| 1879 | |
| 1880 | PyDoc_STRVAR(remove_doc, |
| 1881 | "Remove an element from a set; it must be a member.\n\ |
| 1882 | \n\ |
| 1883 | If the element is not a member, raise a KeyError."); |
| 1884 | |
| 1885 | static PyObject * |
Raymond Hettinger | 06d8cf8 | 2005-07-31 15:36:06 +0000 | [diff] [blame] | 1886 | set_discard(PySetObject *so, PyObject *key) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1887 | { |
Benjamin Peterson | 2b50a01 | 2011-10-30 14:24:44 -0400 | [diff] [blame] | 1888 | PyObject *tmpkey; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1889 | int rv; |
Raymond Hettinger | 0deab62 | 2003-12-13 18:53:18 +0000 | [diff] [blame] | 1890 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1891 | rv = set_discard_key(so, key); |
| 1892 | if (rv == -1) { |
| 1893 | if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError)) |
| 1894 | return NULL; |
| 1895 | PyErr_Clear(); |
Raymond Hettinger | 38bf2cc | 2010-08-06 09:52:17 +0000 | [diff] [blame] | 1896 | tmpkey = make_new_set(&PyFrozenSet_Type, key); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1897 | if (tmpkey == NULL) |
| 1898 | return NULL; |
Petri Lehtinen | e0aa803 | 2011-10-30 14:31:27 +0200 | [diff] [blame] | 1899 | rv = set_discard_key(so, tmpkey); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1900 | Py_DECREF(tmpkey); |
Petri Lehtinen | e0aa803 | 2011-10-30 14:31:27 +0200 | [diff] [blame] | 1901 | if (rv == -1) |
| 1902 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1903 | } |
| 1904 | Py_RETURN_NONE; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1905 | } |
| 1906 | |
| 1907 | PyDoc_STRVAR(discard_doc, |
| 1908 | "Remove an element from a set if it is a member.\n\ |
| 1909 | \n\ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1910 | If the element is not a member, do nothing."); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1911 | |
| 1912 | static PyObject * |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1913 | set_reduce(PySetObject *so) |
| 1914 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1915 | PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 1916 | _Py_IDENTIFIER(__dict__); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1917 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1918 | keys = PySequence_List((PyObject *)so); |
| 1919 | if (keys == NULL) |
| 1920 | goto done; |
| 1921 | args = PyTuple_Pack(1, keys); |
| 1922 | if (args == NULL) |
| 1923 | goto done; |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 1924 | dict = _PyObject_GetAttrId((PyObject *)so, &PyId___dict__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1925 | if (dict == NULL) { |
| 1926 | PyErr_Clear(); |
| 1927 | dict = Py_None; |
| 1928 | Py_INCREF(dict); |
| 1929 | } |
| 1930 | result = PyTuple_Pack(3, Py_TYPE(so), args, dict); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1931 | done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1932 | Py_XDECREF(args); |
| 1933 | Py_XDECREF(keys); |
| 1934 | Py_XDECREF(dict); |
| 1935 | return result; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1936 | } |
| 1937 | |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 1938 | static PyObject * |
| 1939 | set_sizeof(PySetObject *so) |
| 1940 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1941 | Py_ssize_t res; |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 1942 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1943 | res = sizeof(PySetObject); |
| 1944 | if (so->table != so->smalltable) |
| 1945 | res = res + (so->mask + 1) * sizeof(setentry); |
| 1946 | return PyLong_FromSsize_t(res); |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 1947 | } |
| 1948 | |
| 1949 | 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] | 1950 | static int |
| 1951 | set_init(PySetObject *self, PyObject *args, PyObject *kwds) |
| 1952 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1953 | PyObject *iterable = NULL; |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 1954 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1955 | if (!PyAnySet_Check(self)) |
| 1956 | return -1; |
| 1957 | if (PySet_Check(self) && !_PyArg_NoKeywords("set()", kwds)) |
| 1958 | return -1; |
| 1959 | if (!PyArg_UnpackTuple(args, Py_TYPE(self)->tp_name, 0, 1, &iterable)) |
| 1960 | return -1; |
| 1961 | set_clear_internal(self); |
| 1962 | self->hash = -1; |
| 1963 | if (iterable == NULL) |
| 1964 | return 0; |
| 1965 | return set_update_internal(self, iterable); |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 1966 | } |
| 1967 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1968 | static PySequenceMethods set_as_sequence = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1969 | set_len, /* sq_length */ |
| 1970 | 0, /* sq_concat */ |
| 1971 | 0, /* sq_repeat */ |
| 1972 | 0, /* sq_item */ |
| 1973 | 0, /* sq_slice */ |
| 1974 | 0, /* sq_ass_item */ |
| 1975 | 0, /* sq_ass_slice */ |
| 1976 | (objobjproc)set_contains, /* sq_contains */ |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1977 | }; |
| 1978 | |
| 1979 | /* set object ********************************************************/ |
| 1980 | |
Raymond Hettinger | c47e01d | 2005-08-16 10:44:15 +0000 | [diff] [blame] | 1981 | #ifdef Py_DEBUG |
| 1982 | static PyObject *test_c_api(PySetObject *so); |
| 1983 | |
| 1984 | PyDoc_STRVAR(test_c_api_doc, "Exercises C API. Returns True.\n\ |
| 1985 | All is well if assertions don't fail."); |
| 1986 | #endif |
| 1987 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1988 | static PyMethodDef set_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1989 | {"add", (PyCFunction)set_add, METH_O, |
| 1990 | add_doc}, |
| 1991 | {"clear", (PyCFunction)set_clear, METH_NOARGS, |
| 1992 | clear_doc}, |
| 1993 | {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST, |
| 1994 | contains_doc}, |
| 1995 | {"copy", (PyCFunction)set_copy, METH_NOARGS, |
| 1996 | copy_doc}, |
| 1997 | {"discard", (PyCFunction)set_discard, METH_O, |
| 1998 | discard_doc}, |
| 1999 | {"difference", (PyCFunction)set_difference_multi, METH_VARARGS, |
| 2000 | difference_doc}, |
| 2001 | {"difference_update", (PyCFunction)set_difference_update, METH_VARARGS, |
| 2002 | difference_update_doc}, |
| 2003 | {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS, |
| 2004 | intersection_doc}, |
| 2005 | {"intersection_update",(PyCFunction)set_intersection_update_multi, METH_VARARGS, |
| 2006 | intersection_update_doc}, |
| 2007 | {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O, |
| 2008 | isdisjoint_doc}, |
| 2009 | {"issubset", (PyCFunction)set_issubset, METH_O, |
| 2010 | issubset_doc}, |
| 2011 | {"issuperset", (PyCFunction)set_issuperset, METH_O, |
| 2012 | issuperset_doc}, |
| 2013 | {"pop", (PyCFunction)set_pop, METH_NOARGS, |
| 2014 | pop_doc}, |
| 2015 | {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS, |
| 2016 | reduce_doc}, |
| 2017 | {"remove", (PyCFunction)set_remove, METH_O, |
| 2018 | remove_doc}, |
| 2019 | {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS, |
| 2020 | sizeof_doc}, |
| 2021 | {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O, |
| 2022 | symmetric_difference_doc}, |
| 2023 | {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O, |
| 2024 | symmetric_difference_update_doc}, |
Raymond Hettinger | c47e01d | 2005-08-16 10:44:15 +0000 | [diff] [blame] | 2025 | #ifdef Py_DEBUG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2026 | {"test_c_api", (PyCFunction)test_c_api, METH_NOARGS, |
| 2027 | test_c_api_doc}, |
Raymond Hettinger | c47e01d | 2005-08-16 10:44:15 +0000 | [diff] [blame] | 2028 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2029 | {"union", (PyCFunction)set_union, METH_VARARGS, |
| 2030 | union_doc}, |
| 2031 | {"update", (PyCFunction)set_update, METH_VARARGS, |
| 2032 | update_doc}, |
| 2033 | {NULL, NULL} /* sentinel */ |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 2034 | }; |
| 2035 | |
| 2036 | static PyNumberMethods set_as_number = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2037 | 0, /*nb_add*/ |
| 2038 | (binaryfunc)set_sub, /*nb_subtract*/ |
| 2039 | 0, /*nb_multiply*/ |
| 2040 | 0, /*nb_remainder*/ |
| 2041 | 0, /*nb_divmod*/ |
| 2042 | 0, /*nb_power*/ |
| 2043 | 0, /*nb_negative*/ |
| 2044 | 0, /*nb_positive*/ |
| 2045 | 0, /*nb_absolute*/ |
| 2046 | 0, /*nb_bool*/ |
| 2047 | 0, /*nb_invert*/ |
| 2048 | 0, /*nb_lshift*/ |
| 2049 | 0, /*nb_rshift*/ |
| 2050 | (binaryfunc)set_and, /*nb_and*/ |
| 2051 | (binaryfunc)set_xor, /*nb_xor*/ |
| 2052 | (binaryfunc)set_or, /*nb_or*/ |
| 2053 | 0, /*nb_int*/ |
| 2054 | 0, /*nb_reserved*/ |
| 2055 | 0, /*nb_float*/ |
| 2056 | 0, /*nb_inplace_add*/ |
| 2057 | (binaryfunc)set_isub, /*nb_inplace_subtract*/ |
| 2058 | 0, /*nb_inplace_multiply*/ |
| 2059 | 0, /*nb_inplace_remainder*/ |
| 2060 | 0, /*nb_inplace_power*/ |
| 2061 | 0, /*nb_inplace_lshift*/ |
| 2062 | 0, /*nb_inplace_rshift*/ |
| 2063 | (binaryfunc)set_iand, /*nb_inplace_and*/ |
| 2064 | (binaryfunc)set_ixor, /*nb_inplace_xor*/ |
| 2065 | (binaryfunc)set_ior, /*nb_inplace_or*/ |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 2066 | }; |
| 2067 | |
| 2068 | PyDoc_STRVAR(set_doc, |
Ezio Melotti | 7f807b7 | 2010-03-01 04:08:34 +0000 | [diff] [blame] | 2069 | "set() -> new empty set object\n\ |
| 2070 | set(iterable) -> new set object\n\ |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 2071 | \n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2072 | Build an unordered collection of unique elements."); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 2073 | |
| 2074 | PyTypeObject PySet_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2075 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 2076 | "set", /* tp_name */ |
| 2077 | sizeof(PySetObject), /* tp_basicsize */ |
| 2078 | 0, /* tp_itemsize */ |
| 2079 | /* methods */ |
| 2080 | (destructor)set_dealloc, /* tp_dealloc */ |
| 2081 | 0, /* tp_print */ |
| 2082 | 0, /* tp_getattr */ |
| 2083 | 0, /* tp_setattr */ |
| 2084 | 0, /* tp_reserved */ |
| 2085 | (reprfunc)set_repr, /* tp_repr */ |
| 2086 | &set_as_number, /* tp_as_number */ |
| 2087 | &set_as_sequence, /* tp_as_sequence */ |
| 2088 | 0, /* tp_as_mapping */ |
Georg Brandl | 00da4e0 | 2010-10-18 07:32:48 +0000 | [diff] [blame] | 2089 | PyObject_HashNotImplemented, /* tp_hash */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2090 | 0, /* tp_call */ |
| 2091 | 0, /* tp_str */ |
| 2092 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 2093 | 0, /* tp_setattro */ |
| 2094 | 0, /* tp_as_buffer */ |
| 2095 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 2096 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 2097 | set_doc, /* tp_doc */ |
| 2098 | (traverseproc)set_traverse, /* tp_traverse */ |
| 2099 | (inquiry)set_clear_internal, /* tp_clear */ |
| 2100 | (richcmpfunc)set_richcompare, /* tp_richcompare */ |
Georg Brandl | 00da4e0 | 2010-10-18 07:32:48 +0000 | [diff] [blame] | 2101 | offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */ |
| 2102 | (getiterfunc)set_iter, /* tp_iter */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2103 | 0, /* tp_iternext */ |
| 2104 | set_methods, /* tp_methods */ |
| 2105 | 0, /* tp_members */ |
| 2106 | 0, /* tp_getset */ |
| 2107 | 0, /* tp_base */ |
| 2108 | 0, /* tp_dict */ |
| 2109 | 0, /* tp_descr_get */ |
| 2110 | 0, /* tp_descr_set */ |
| 2111 | 0, /* tp_dictoffset */ |
| 2112 | (initproc)set_init, /* tp_init */ |
| 2113 | PyType_GenericAlloc, /* tp_alloc */ |
| 2114 | set_new, /* tp_new */ |
| 2115 | PyObject_GC_Del, /* tp_free */ |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 2116 | }; |
| 2117 | |
| 2118 | /* frozenset object ********************************************************/ |
| 2119 | |
| 2120 | |
| 2121 | static PyMethodDef frozenset_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2122 | {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST, |
| 2123 | contains_doc}, |
| 2124 | {"copy", (PyCFunction)frozenset_copy, METH_NOARGS, |
| 2125 | copy_doc}, |
| 2126 | {"difference", (PyCFunction)set_difference_multi, METH_VARARGS, |
| 2127 | difference_doc}, |
| 2128 | {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS, |
| 2129 | intersection_doc}, |
| 2130 | {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O, |
| 2131 | isdisjoint_doc}, |
| 2132 | {"issubset", (PyCFunction)set_issubset, METH_O, |
| 2133 | issubset_doc}, |
| 2134 | {"issuperset", (PyCFunction)set_issuperset, METH_O, |
| 2135 | issuperset_doc}, |
| 2136 | {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS, |
| 2137 | reduce_doc}, |
| 2138 | {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS, |
| 2139 | sizeof_doc}, |
| 2140 | {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O, |
| 2141 | symmetric_difference_doc}, |
| 2142 | {"union", (PyCFunction)set_union, METH_VARARGS, |
| 2143 | union_doc}, |
| 2144 | {NULL, NULL} /* sentinel */ |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 2145 | }; |
| 2146 | |
| 2147 | static PyNumberMethods frozenset_as_number = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2148 | 0, /*nb_add*/ |
| 2149 | (binaryfunc)set_sub, /*nb_subtract*/ |
| 2150 | 0, /*nb_multiply*/ |
| 2151 | 0, /*nb_remainder*/ |
| 2152 | 0, /*nb_divmod*/ |
| 2153 | 0, /*nb_power*/ |
| 2154 | 0, /*nb_negative*/ |
| 2155 | 0, /*nb_positive*/ |
| 2156 | 0, /*nb_absolute*/ |
| 2157 | 0, /*nb_bool*/ |
| 2158 | 0, /*nb_invert*/ |
| 2159 | 0, /*nb_lshift*/ |
| 2160 | 0, /*nb_rshift*/ |
| 2161 | (binaryfunc)set_and, /*nb_and*/ |
| 2162 | (binaryfunc)set_xor, /*nb_xor*/ |
| 2163 | (binaryfunc)set_or, /*nb_or*/ |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 2164 | }; |
| 2165 | |
| 2166 | PyDoc_STRVAR(frozenset_doc, |
Ezio Melotti | 7f807b7 | 2010-03-01 04:08:34 +0000 | [diff] [blame] | 2167 | "frozenset() -> empty frozenset object\n\ |
| 2168 | frozenset(iterable) -> frozenset object\n\ |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 2169 | \n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2170 | Build an immutable unordered collection of unique elements."); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 2171 | |
| 2172 | PyTypeObject PyFrozenSet_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2173 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 2174 | "frozenset", /* tp_name */ |
| 2175 | sizeof(PySetObject), /* tp_basicsize */ |
| 2176 | 0, /* tp_itemsize */ |
| 2177 | /* methods */ |
| 2178 | (destructor)set_dealloc, /* tp_dealloc */ |
| 2179 | 0, /* tp_print */ |
| 2180 | 0, /* tp_getattr */ |
| 2181 | 0, /* tp_setattr */ |
| 2182 | 0, /* tp_reserved */ |
| 2183 | (reprfunc)set_repr, /* tp_repr */ |
| 2184 | &frozenset_as_number, /* tp_as_number */ |
| 2185 | &set_as_sequence, /* tp_as_sequence */ |
| 2186 | 0, /* tp_as_mapping */ |
| 2187 | frozenset_hash, /* tp_hash */ |
| 2188 | 0, /* tp_call */ |
| 2189 | 0, /* tp_str */ |
| 2190 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 2191 | 0, /* tp_setattro */ |
| 2192 | 0, /* tp_as_buffer */ |
| 2193 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 2194 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 2195 | frozenset_doc, /* tp_doc */ |
| 2196 | (traverseproc)set_traverse, /* tp_traverse */ |
| 2197 | (inquiry)set_clear_internal, /* tp_clear */ |
| 2198 | (richcmpfunc)set_richcompare, /* tp_richcompare */ |
| 2199 | offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */ |
| 2200 | (getiterfunc)set_iter, /* tp_iter */ |
| 2201 | 0, /* tp_iternext */ |
| 2202 | frozenset_methods, /* tp_methods */ |
| 2203 | 0, /* tp_members */ |
| 2204 | 0, /* tp_getset */ |
| 2205 | 0, /* tp_base */ |
| 2206 | 0, /* tp_dict */ |
| 2207 | 0, /* tp_descr_get */ |
| 2208 | 0, /* tp_descr_set */ |
| 2209 | 0, /* tp_dictoffset */ |
| 2210 | 0, /* tp_init */ |
| 2211 | PyType_GenericAlloc, /* tp_alloc */ |
| 2212 | frozenset_new, /* tp_new */ |
| 2213 | PyObject_GC_Del, /* tp_free */ |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 2214 | }; |
Raymond Hettinger | beb3101 | 2005-08-16 03:47:52 +0000 | [diff] [blame] | 2215 | |
| 2216 | |
| 2217 | /***** C API functions *************************************************/ |
| 2218 | |
| 2219 | PyObject * |
| 2220 | PySet_New(PyObject *iterable) |
| 2221 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2222 | return make_new_set(&PySet_Type, iterable); |
Raymond Hettinger | beb3101 | 2005-08-16 03:47:52 +0000 | [diff] [blame] | 2223 | } |
| 2224 | |
| 2225 | PyObject * |
| 2226 | PyFrozenSet_New(PyObject *iterable) |
| 2227 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2228 | return make_new_set(&PyFrozenSet_Type, iterable); |
Raymond Hettinger | beb3101 | 2005-08-16 03:47:52 +0000 | [diff] [blame] | 2229 | } |
| 2230 | |
Neal Norwitz | 8c49c82 | 2006-03-04 18:41:19 +0000 | [diff] [blame] | 2231 | Py_ssize_t |
Raymond Hettinger | c47e01d | 2005-08-16 10:44:15 +0000 | [diff] [blame] | 2232 | PySet_Size(PyObject *anyset) |
| 2233 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2234 | if (!PyAnySet_Check(anyset)) { |
| 2235 | PyErr_BadInternalCall(); |
| 2236 | return -1; |
| 2237 | } |
| 2238 | return PySet_GET_SIZE(anyset); |
Raymond Hettinger | c47e01d | 2005-08-16 10:44:15 +0000 | [diff] [blame] | 2239 | } |
| 2240 | |
| 2241 | int |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2242 | PySet_Clear(PyObject *set) |
| 2243 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2244 | if (!PySet_Check(set)) { |
| 2245 | PyErr_BadInternalCall(); |
| 2246 | return -1; |
| 2247 | } |
| 2248 | return set_clear_internal((PySetObject *)set); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2249 | } |
| 2250 | |
| 2251 | int |
Raymond Hettinger | beb3101 | 2005-08-16 03:47:52 +0000 | [diff] [blame] | 2252 | PySet_Contains(PyObject *anyset, PyObject *key) |
| 2253 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2254 | if (!PyAnySet_Check(anyset)) { |
| 2255 | PyErr_BadInternalCall(); |
| 2256 | return -1; |
| 2257 | } |
| 2258 | return set_contains_key((PySetObject *)anyset, key); |
Raymond Hettinger | beb3101 | 2005-08-16 03:47:52 +0000 | [diff] [blame] | 2259 | } |
| 2260 | |
| 2261 | int |
Raymond Hettinger | c47e01d | 2005-08-16 10:44:15 +0000 | [diff] [blame] | 2262 | PySet_Discard(PyObject *set, 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(set)) { |
| 2265 | PyErr_BadInternalCall(); |
| 2266 | return -1; |
| 2267 | } |
| 2268 | return set_discard_key((PySetObject *)set, key); |
Raymond Hettinger | beb3101 | 2005-08-16 03:47:52 +0000 | [diff] [blame] | 2269 | } |
| 2270 | |
| 2271 | int |
Christian Heimes | fd66e51 | 2008-01-29 12:18:50 +0000 | [diff] [blame] | 2272 | PySet_Add(PyObject *anyset, PyObject *key) |
Raymond Hettinger | beb3101 | 2005-08-16 03:47:52 +0000 | [diff] [blame] | 2273 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2274 | if (!PySet_Check(anyset) && |
| 2275 | (!PyFrozenSet_Check(anyset) || Py_REFCNT(anyset) != 1)) { |
| 2276 | PyErr_BadInternalCall(); |
| 2277 | return -1; |
| 2278 | } |
| 2279 | return set_add_key((PySetObject *)anyset, key); |
Raymond Hettinger | beb3101 | 2005-08-16 03:47:52 +0000 | [diff] [blame] | 2280 | } |
| 2281 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2282 | int |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 2283 | _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] | 2284 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2285 | setentry *entry; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2286 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2287 | if (!PyAnySet_Check(set)) { |
| 2288 | PyErr_BadInternalCall(); |
| 2289 | return -1; |
| 2290 | } |
| 2291 | if (set_next((PySetObject *)set, pos, &entry) == 0) |
| 2292 | return 0; |
| 2293 | *key = entry->key; |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 2294 | *hash = entry->hash; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2295 | return 1; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2296 | } |
| 2297 | |
Raymond Hettinger | beb3101 | 2005-08-16 03:47:52 +0000 | [diff] [blame] | 2298 | PyObject * |
| 2299 | PySet_Pop(PyObject *set) |
| 2300 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2301 | if (!PySet_Check(set)) { |
| 2302 | PyErr_BadInternalCall(); |
| 2303 | return NULL; |
| 2304 | } |
| 2305 | return set_pop((PySetObject *)set); |
Raymond Hettinger | beb3101 | 2005-08-16 03:47:52 +0000 | [diff] [blame] | 2306 | } |
Raymond Hettinger | c47e01d | 2005-08-16 10:44:15 +0000 | [diff] [blame] | 2307 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2308 | int |
| 2309 | _PySet_Update(PyObject *set, PyObject *iterable) |
| 2310 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2311 | if (!PySet_Check(set)) { |
| 2312 | PyErr_BadInternalCall(); |
| 2313 | return -1; |
| 2314 | } |
| 2315 | return set_update_internal((PySetObject *)set, iterable); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2316 | } |
Raymond Hettinger | c47e01d | 2005-08-16 10:44:15 +0000 | [diff] [blame] | 2317 | |
Raymond Hettinger | e259f13 | 2013-12-15 11:56:14 -0800 | [diff] [blame] | 2318 | /* Exported for the gdb plugin's benefit. */ |
| 2319 | PyObject *_PySet_Dummy = dummy; |
| 2320 | |
Raymond Hettinger | c47e01d | 2005-08-16 10:44:15 +0000 | [diff] [blame] | 2321 | #ifdef Py_DEBUG |
| 2322 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2323 | /* Test code to be called with any three element set. |
Raymond Hettinger | c47e01d | 2005-08-16 10:44:15 +0000 | [diff] [blame] | 2324 | Returns True and original set is restored. */ |
| 2325 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2326 | #define assertRaises(call_return_value, exception) \ |
| 2327 | do { \ |
| 2328 | assert(call_return_value); \ |
| 2329 | assert(PyErr_ExceptionMatches(exception)); \ |
| 2330 | PyErr_Clear(); \ |
| 2331 | } while(0) |
Raymond Hettinger | c47e01d | 2005-08-16 10:44:15 +0000 | [diff] [blame] | 2332 | |
| 2333 | static PyObject * |
| 2334 | test_c_api(PySetObject *so) |
| 2335 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2336 | Py_ssize_t count; |
| 2337 | char *s; |
| 2338 | Py_ssize_t i; |
Raymond Hettinger | 583cd03 | 2013-09-07 22:06:35 -0700 | [diff] [blame] | 2339 | PyObject *elem=NULL, *dup=NULL, *t, *f, *dup2, *x=NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2340 | PyObject *ob = (PyObject *)so; |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 2341 | Py_hash_t hash; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2342 | PyObject *str; |
Raymond Hettinger | c47e01d | 2005-08-16 10:44:15 +0000 | [diff] [blame] | 2343 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2344 | /* Verify preconditions */ |
| 2345 | assert(PyAnySet_Check(ob)); |
| 2346 | assert(PyAnySet_CheckExact(ob)); |
| 2347 | assert(!PyFrozenSet_CheckExact(ob)); |
Victor Stinner | 08b36bd | 2010-03-13 00:19:17 +0000 | [diff] [blame] | 2348 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2349 | /* so.clear(); so |= set("abc"); */ |
| 2350 | str = PyUnicode_FromString("abc"); |
| 2351 | if (str == NULL) |
| 2352 | return NULL; |
| 2353 | set_clear_internal(so); |
Raymond Hettinger | 5af9e13 | 2015-05-13 01:44:36 -0700 | [diff] [blame] | 2354 | if (set_update_internal(so, str)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2355 | Py_DECREF(str); |
| 2356 | return NULL; |
| 2357 | } |
| 2358 | Py_DECREF(str); |
Victor Stinner | 08b36bd | 2010-03-13 00:19:17 +0000 | [diff] [blame] | 2359 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2360 | /* Exercise type/size checks */ |
| 2361 | assert(PySet_Size(ob) == 3); |
| 2362 | assert(PySet_GET_SIZE(ob) == 3); |
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 | /* Raise TypeError for non-iterable constructor arguments */ |
| 2365 | assertRaises(PySet_New(Py_None) == NULL, PyExc_TypeError); |
| 2366 | assertRaises(PyFrozenSet_New(Py_None) == NULL, PyExc_TypeError); |
Raymond Hettinger | c47e01d | 2005-08-16 10:44:15 +0000 | [diff] [blame] | 2367 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2368 | /* Raise TypeError for unhashable key */ |
| 2369 | dup = PySet_New(ob); |
| 2370 | assertRaises(PySet_Discard(ob, dup) == -1, PyExc_TypeError); |
| 2371 | assertRaises(PySet_Contains(ob, dup) == -1, PyExc_TypeError); |
| 2372 | assertRaises(PySet_Add(ob, dup) == -1, PyExc_TypeError); |
Raymond Hettinger | c47e01d | 2005-08-16 10:44:15 +0000 | [diff] [blame] | 2373 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2374 | /* Exercise successful pop, contains, add, and discard */ |
| 2375 | elem = PySet_Pop(ob); |
| 2376 | assert(PySet_Contains(ob, elem) == 0); |
| 2377 | assert(PySet_GET_SIZE(ob) == 2); |
| 2378 | assert(PySet_Add(ob, elem) == 0); |
| 2379 | assert(PySet_Contains(ob, elem) == 1); |
| 2380 | assert(PySet_GET_SIZE(ob) == 3); |
| 2381 | assert(PySet_Discard(ob, elem) == 1); |
| 2382 | assert(PySet_GET_SIZE(ob) == 2); |
| 2383 | assert(PySet_Discard(ob, elem) == 0); |
| 2384 | assert(PySet_GET_SIZE(ob) == 2); |
Raymond Hettinger | c47e01d | 2005-08-16 10:44:15 +0000 | [diff] [blame] | 2385 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2386 | /* Exercise clear */ |
| 2387 | dup2 = PySet_New(dup); |
| 2388 | assert(PySet_Clear(dup2) == 0); |
| 2389 | assert(PySet_Size(dup2) == 0); |
| 2390 | Py_DECREF(dup2); |
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 | /* Raise SystemError on clear or update of frozen set */ |
| 2393 | f = PyFrozenSet_New(dup); |
| 2394 | assertRaises(PySet_Clear(f) == -1, PyExc_SystemError); |
| 2395 | assertRaises(_PySet_Update(f, dup) == -1, PyExc_SystemError); |
| 2396 | assert(PySet_Add(f, elem) == 0); |
| 2397 | Py_INCREF(f); |
| 2398 | assertRaises(PySet_Add(f, elem) == -1, PyExc_SystemError); |
| 2399 | Py_DECREF(f); |
| 2400 | Py_DECREF(f); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2401 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2402 | /* Exercise direct iteration */ |
| 2403 | i = 0, count = 0; |
| 2404 | while (_PySet_NextEntry((PyObject *)dup, &i, &x, &hash)) { |
| 2405 | s = _PyUnicode_AsString(x); |
| 2406 | assert(s && (s[0] == 'a' || s[0] == 'b' || s[0] == 'c')); |
| 2407 | count++; |
| 2408 | } |
| 2409 | assert(count == 3); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2410 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2411 | /* Exercise updates */ |
| 2412 | dup2 = PySet_New(NULL); |
| 2413 | assert(_PySet_Update(dup2, dup) == 0); |
| 2414 | assert(PySet_Size(dup2) == 3); |
| 2415 | assert(_PySet_Update(dup2, dup) == 0); |
| 2416 | assert(PySet_Size(dup2) == 3); |
| 2417 | Py_DECREF(dup2); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2418 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2419 | /* Raise SystemError when self argument is not a set or frozenset. */ |
| 2420 | t = PyTuple_New(0); |
| 2421 | assertRaises(PySet_Size(t) == -1, PyExc_SystemError); |
| 2422 | assertRaises(PySet_Contains(t, elem) == -1, PyExc_SystemError); |
| 2423 | Py_DECREF(t); |
Raymond Hettinger | c47e01d | 2005-08-16 10:44:15 +0000 | [diff] [blame] | 2424 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2425 | /* Raise SystemError when self argument is not a set. */ |
| 2426 | f = PyFrozenSet_New(dup); |
| 2427 | assert(PySet_Size(f) == 3); |
| 2428 | assert(PyFrozenSet_CheckExact(f)); |
| 2429 | assertRaises(PySet_Discard(f, elem) == -1, PyExc_SystemError); |
| 2430 | assertRaises(PySet_Pop(f) == NULL, PyExc_SystemError); |
| 2431 | Py_DECREF(f); |
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 | /* Raise KeyError when popping from an empty set */ |
| 2434 | assert(PyNumber_InPlaceSubtract(ob, ob) == ob); |
| 2435 | Py_DECREF(ob); |
| 2436 | assert(PySet_GET_SIZE(ob) == 0); |
| 2437 | assertRaises(PySet_Pop(ob) == NULL, PyExc_KeyError); |
Raymond Hettinger | c47e01d | 2005-08-16 10:44:15 +0000 | [diff] [blame] | 2438 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2439 | /* Restore the set from the copy using the PyNumber API */ |
| 2440 | assert(PyNumber_InPlaceOr(ob, dup) == ob); |
| 2441 | Py_DECREF(ob); |
Raymond Hettinger | c47e01d | 2005-08-16 10:44:15 +0000 | [diff] [blame] | 2442 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2443 | /* Verify constructors accept NULL arguments */ |
| 2444 | f = PySet_New(NULL); |
| 2445 | assert(f != NULL); |
| 2446 | assert(PySet_GET_SIZE(f) == 0); |
| 2447 | Py_DECREF(f); |
| 2448 | f = PyFrozenSet_New(NULL); |
| 2449 | assert(f != NULL); |
| 2450 | assert(PyFrozenSet_CheckExact(f)); |
| 2451 | assert(PySet_GET_SIZE(f) == 0); |
| 2452 | Py_DECREF(f); |
Raymond Hettinger | c47e01d | 2005-08-16 10:44:15 +0000 | [diff] [blame] | 2453 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2454 | Py_DECREF(elem); |
| 2455 | Py_DECREF(dup); |
| 2456 | Py_RETURN_TRUE; |
Raymond Hettinger | c47e01d | 2005-08-16 10:44:15 +0000 | [diff] [blame] | 2457 | } |
| 2458 | |
Raymond Hettinger | 9bda1d6 | 2005-09-16 07:14:21 +0000 | [diff] [blame] | 2459 | #undef assertRaises |
| 2460 | |
Raymond Hettinger | c47e01d | 2005-08-16 10:44:15 +0000 | [diff] [blame] | 2461 | #endif |
Raymond Hettinger | bfc1e1a | 2013-08-23 03:22:15 -0500 | [diff] [blame] | 2462 | |
| 2463 | /***** Dummy Struct *************************************************/ |
| 2464 | |
| 2465 | static PyObject * |
| 2466 | dummy_repr(PyObject *op) |
| 2467 | { |
| 2468 | return PyUnicode_FromString("<dummy key>"); |
| 2469 | } |
| 2470 | |
| 2471 | static void |
| 2472 | dummy_dealloc(PyObject* ignore) |
| 2473 | { |
| 2474 | Py_FatalError("deallocating <dummy key>"); |
| 2475 | } |
| 2476 | |
| 2477 | static PyTypeObject _PySetDummy_Type = { |
| 2478 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 2479 | "<dummy key> type", |
| 2480 | 0, |
| 2481 | 0, |
| 2482 | dummy_dealloc, /*tp_dealloc*/ /*never called*/ |
| 2483 | 0, /*tp_print*/ |
| 2484 | 0, /*tp_getattr*/ |
| 2485 | 0, /*tp_setattr*/ |
| 2486 | 0, /*tp_reserved*/ |
| 2487 | dummy_repr, /*tp_repr*/ |
| 2488 | 0, /*tp_as_number*/ |
| 2489 | 0, /*tp_as_sequence*/ |
| 2490 | 0, /*tp_as_mapping*/ |
| 2491 | 0, /*tp_hash */ |
| 2492 | 0, /*tp_call */ |
| 2493 | 0, /*tp_str */ |
| 2494 | 0, /*tp_getattro */ |
| 2495 | 0, /*tp_setattro */ |
| 2496 | 0, /*tp_as_buffer */ |
| 2497 | Py_TPFLAGS_DEFAULT, /*tp_flags */ |
| 2498 | }; |
| 2499 | |
| 2500 | static PyObject _dummy_struct = { |
| 2501 | _PyObject_EXTRA_INIT |
| 2502 | 2, &_PySetDummy_Type |
| 2503 | }; |
| 2504 | |