setobject.c 73 KB

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