66extern "C" {
77#endif
88
9+ #ifndef Py_LIMITED_API
910
10- /*
11- There are three kinds of slots in the table:
11+ /* There are three kinds of entries in the table:
1212
13131. Unused: key == NULL
14142. Active: key != NULL and key != dummy
@@ -18,7 +18,7 @@ Note: .pop() abuses the hash field of an Unused or Dummy slot to
1818hold a search finger. The hash field of Unused or Dummy slots has
1919no meaning otherwise.
2020*/
21- #ifndef Py_LIMITED_API
21+
2222#define PySet_MINSIZE 8
2323
2424typedef struct {
@@ -27,51 +27,65 @@ typedef struct {
2727 Py_hash_t hash ;
2828} setentry ;
2929
30+ /* The SetObject data structure is shared by set and frozenset objects.
31+
32+ Invariant for sets:
33+ - hash is -1
34+
35+ Invariants for frozensets:
36+ - data is immutable.
37+ - hash is the hash of the frozenset or -1 if not computed yet.
3038
31- /*
32- This data structure is shared by set and frozenset objects.
3339*/
3440
35- typedef struct _setobject PySetObject ;
36- struct _setobject {
41+ typedef struct _setobject {
3742 PyObject_HEAD
3843
39- Py_ssize_t fill ; /* # Active + # Dummy */
40- Py_ssize_t used ; /* # Active */
44+ Py_ssize_t fill ; /* Number active and dummy entries */
45+ Py_ssize_t used ; /* Number active entries */
4146
4247 /* The table contains mask + 1 slots, and that's a power of 2.
4348 * We store the mask instead of the size because the mask is more
4449 * frequently needed.
4550 */
4651 Py_ssize_t mask ;
4752
48- /* table points to smalltable for small tables, else to
49- * additional malloc'ed memory. table is never NULL! This rule
50- * saves repeated runtime null-tests.
53+ /* The table points to a fixed-size smalltable for small tables
54+ * or to additional malloc'ed memory for bigger tables.
55+ * The table pointer is never NULL which saves us from repeated
56+ * runtime null-tests.
5157 */
5258 setentry * table ;
53- setentry * (* lookup )(PySetObject * so , PyObject * key , Py_hash_t hash );
54- Py_hash_t hash ; /* only used by frozenset objects */
59+ setentry * (* lookup )(struct _setobject * so , PyObject * key , Py_hash_t hash );
60+ Py_hash_t hash ; /* Only used by frozenset objects */
5561 setentry smalltable [PySet_MINSIZE ];
5662
5763 PyObject * weakreflist ; /* List of weak references */
58- };
59- #endif /* Py_LIMITED_API */
64+ } PySetObject ;
65+
66+ #define PySet_GET_SIZE (so ) (((PySetObject *)(so))->used)
67+
68+ PyAPI_DATA (PyObject * ) _PySet_Dummy ;
69+
70+ PyAPI_FUNC (int ) _PySet_NextEntry (PyObject * set , Py_ssize_t * pos , PyObject * * key , Py_hash_t * hash );
71+ PyAPI_FUNC (int ) _PySet_Update (PyObject * set , PyObject * iterable );
72+ PyAPI_FUNC (int ) PySet_ClearFreeList (void );
73+
74+ #endif /* Section excluded by Py_LIMITED_API */
6075
6176PyAPI_DATA (PyTypeObject ) PySet_Type ;
6277PyAPI_DATA (PyTypeObject ) PyFrozenSet_Type ;
6378PyAPI_DATA (PyTypeObject ) PySetIter_Type ;
64- #ifndef Py_LIMITED_API
65- PyAPI_DATA (PyObject * ) _PySet_Dummy ;
66- #endif
6779
80+ PyAPI_FUNC (PyObject * ) PySet_New (PyObject * );
81+ PyAPI_FUNC (PyObject * ) PyFrozenSet_New (PyObject * );
6882
69- /* Invariants for frozensets:
70- * data is immutable.
71- * hash is the hash of the frozenset or -1 if not computed yet.
72- * Invariants for sets:
73- * hash is -1
74- */
83+ PyAPI_FUNC ( int ) PySet_Add ( PyObject * set , PyObject * key );
84+ PyAPI_FUNC ( int ) PySet_Clear ( PyObject * set );
85+ PyAPI_FUNC ( int ) PySet_Contains ( PyObject * anyset , PyObject * key );
86+ PyAPI_FUNC ( int ) PySet_Discard ( PyObject * set , PyObject * key );
87+ PyAPI_FUNC ( PyObject * ) PySet_Pop ( PyObject * set );
88+ PyAPI_FUNC ( Py_ssize_t ) PySet_Size ( PyObject * anyset );
7589
7690#define PyFrozenSet_CheckExact (ob ) (Py_TYPE(ob) == &PyFrozenSet_Type)
7791#define PyAnySet_CheckExact (ob ) \
@@ -87,26 +101,6 @@ PyAPI_DATA(PyObject *) _PySet_Dummy;
87101 (Py_TYPE(ob) == &PyFrozenSet_Type || \
88102 PyType_IsSubtype(Py_TYPE(ob), &PyFrozenSet_Type))
89103
90- PyAPI_FUNC (PyObject * ) PySet_New (PyObject * );
91- PyAPI_FUNC (PyObject * ) PyFrozenSet_New (PyObject * );
92- PyAPI_FUNC (Py_ssize_t ) PySet_Size (PyObject * anyset );
93- #ifndef Py_LIMITED_API
94- #define PySet_GET_SIZE (so ) (((PySetObject *)(so))->used)
95- #endif
96- PyAPI_FUNC (int ) PySet_Clear (PyObject * set );
97- PyAPI_FUNC (int ) PySet_Contains (PyObject * anyset , PyObject * key );
98- PyAPI_FUNC (int ) PySet_Discard (PyObject * set , PyObject * key );
99- PyAPI_FUNC (int ) PySet_Add (PyObject * set , PyObject * key );
100- #ifndef Py_LIMITED_API
101- PyAPI_FUNC (int ) _PySet_NextEntry (PyObject * set , Py_ssize_t * pos , PyObject * * key , Py_hash_t * hash );
102- #endif
103- PyAPI_FUNC (PyObject * ) PySet_Pop (PyObject * set );
104- #ifndef Py_LIMITED_API
105- PyAPI_FUNC (int ) _PySet_Update (PyObject * set , PyObject * iterable );
106-
107- PyAPI_FUNC (int ) PySet_ClearFreeList (void );
108- #endif
109-
110104#ifdef __cplusplus
111105}
112106#endif
0 commit comments