17db96d56Sopenharmony_ci#ifndef Py_CPYTHON_SETOBJECT_H
27db96d56Sopenharmony_ci#  error "this header file must not be included directly"
37db96d56Sopenharmony_ci#endif
47db96d56Sopenharmony_ci
57db96d56Sopenharmony_ci/* There are three kinds of entries in the table:
67db96d56Sopenharmony_ci
77db96d56Sopenharmony_ci1. Unused:  key == NULL and hash == 0
87db96d56Sopenharmony_ci2. Dummy:   key == dummy and hash == -1
97db96d56Sopenharmony_ci3. Active:  key != NULL and key != dummy and hash != -1
107db96d56Sopenharmony_ci
117db96d56Sopenharmony_ciThe hash field of Unused slots is always zero.
127db96d56Sopenharmony_ci
137db96d56Sopenharmony_ciThe hash field of Dummy slots are set to -1
147db96d56Sopenharmony_cimeaning that dummy entries can be detected by
157db96d56Sopenharmony_cieither entry->key==dummy or by entry->hash==-1.
167db96d56Sopenharmony_ci*/
177db96d56Sopenharmony_ci
187db96d56Sopenharmony_ci#define PySet_MINSIZE 8
197db96d56Sopenharmony_ci
207db96d56Sopenharmony_citypedef struct {
217db96d56Sopenharmony_ci    PyObject *key;
227db96d56Sopenharmony_ci    Py_hash_t hash;             /* Cached hash code of the key */
237db96d56Sopenharmony_ci} setentry;
247db96d56Sopenharmony_ci
257db96d56Sopenharmony_ci/* The SetObject data structure is shared by set and frozenset objects.
267db96d56Sopenharmony_ci
277db96d56Sopenharmony_ciInvariant for sets:
287db96d56Sopenharmony_ci - hash is -1
297db96d56Sopenharmony_ci
307db96d56Sopenharmony_ciInvariants for frozensets:
317db96d56Sopenharmony_ci - data is immutable.
327db96d56Sopenharmony_ci - hash is the hash of the frozenset or -1 if not computed yet.
337db96d56Sopenharmony_ci
347db96d56Sopenharmony_ci*/
357db96d56Sopenharmony_ci
367db96d56Sopenharmony_citypedef struct {
377db96d56Sopenharmony_ci    PyObject_HEAD
387db96d56Sopenharmony_ci
397db96d56Sopenharmony_ci    Py_ssize_t fill;            /* Number active and dummy entries*/
407db96d56Sopenharmony_ci    Py_ssize_t used;            /* Number active entries */
417db96d56Sopenharmony_ci
427db96d56Sopenharmony_ci    /* The table contains mask + 1 slots, and that's a power of 2.
437db96d56Sopenharmony_ci     * We store the mask instead of the size because the mask is more
447db96d56Sopenharmony_ci     * frequently needed.
457db96d56Sopenharmony_ci     */
467db96d56Sopenharmony_ci    Py_ssize_t mask;
477db96d56Sopenharmony_ci
487db96d56Sopenharmony_ci    /* The table points to a fixed-size smalltable for small tables
497db96d56Sopenharmony_ci     * or to additional malloc'ed memory for bigger tables.
507db96d56Sopenharmony_ci     * The table pointer is never NULL which saves us from repeated
517db96d56Sopenharmony_ci     * runtime null-tests.
527db96d56Sopenharmony_ci     */
537db96d56Sopenharmony_ci    setentry *table;
547db96d56Sopenharmony_ci    Py_hash_t hash;             /* Only used by frozenset objects */
557db96d56Sopenharmony_ci    Py_ssize_t finger;          /* Search finger for pop() */
567db96d56Sopenharmony_ci
577db96d56Sopenharmony_ci    setentry smalltable[PySet_MINSIZE];
587db96d56Sopenharmony_ci    PyObject *weakreflist;      /* List of weak references */
597db96d56Sopenharmony_ci} PySetObject;
607db96d56Sopenharmony_ci
617db96d56Sopenharmony_ci#define PySet_GET_SIZE(so) \
627db96d56Sopenharmony_ci    (assert(PyAnySet_Check(so)), (((PySetObject *)(so))->used))
637db96d56Sopenharmony_ci
647db96d56Sopenharmony_ciPyAPI_DATA(PyObject *) _PySet_Dummy;
657db96d56Sopenharmony_ci
667db96d56Sopenharmony_ciPyAPI_FUNC(int) _PySet_NextEntry(PyObject *set, Py_ssize_t *pos, PyObject **key, Py_hash_t *hash);
677db96d56Sopenharmony_ciPyAPI_FUNC(int) _PySet_Update(PyObject *set, PyObject *iterable);
68