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