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