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