1/* Set object interface */ 2 3#ifndef Py_SETOBJECT_H 4#define Py_SETOBJECT_H 5#ifdef __cplusplus 6extern "C" { 7#endif 8 9 10/* 11There are three kinds of slots in the table: 12 131. Unused: key == NULL 142. Active: key != NULL and key != dummy 153. Dummy: key == dummy 16 17Note: .pop() abuses the hash field of an Unused or Dummy slot to 18hold a search finger. The hash field of Unused or Dummy slots has 19no meaning otherwise. 20*/ 21 22#define PySet_MINSIZE 8 23 24typedef struct { 25 long hash; /* cached hash code for the entry key */ 26 PyObject *key; 27} setentry; 28 29 30/* 31This data structure is shared by set and frozenset objects. 32*/ 33 34typedef struct _setobject PySetObject; 35struct _setobject { 36 PyObject_HEAD 37 38 Py_ssize_t fill; /* # Active + # Dummy */ 39 Py_ssize_t used; /* # Active */ 40 41 /* The table contains mask + 1 slots, and that's a power of 2. 42 * We store the mask instead of the size because the mask is more 43 * frequently needed. 44 */ 45 Py_ssize_t mask; 46 47 /* table points to smalltable for small tables, else to 48 * additional malloc'ed memory. table is never NULL! This rule 49 * saves repeated runtime null-tests. 50 */ 51 setentry *table; 52 setentry *(*lookup)(PySetObject *so, PyObject *key, long hash); 53 setentry smalltable[PySet_MINSIZE]; 54 55 long hash; /* only used by frozenset objects */ 56 PyObject *weakreflist; /* List of weak references */ 57}; 58 59PyAPI_DATA(PyTypeObject) PySet_Type; 60PyAPI_DATA(PyTypeObject) PyFrozenSet_Type; 61 62/* Invariants for frozensets: 63 * data is immutable. 64 * hash is the hash of the frozenset or -1 if not computed yet. 65 * Invariants for sets: 66 * hash is -1 67 */ 68 69#define PyFrozenSet_CheckExact(ob) (Py_TYPE(ob) == &PyFrozenSet_Type) 70#define PyAnySet_CheckExact(ob) \ 71 (Py_TYPE(ob) == &PySet_Type || Py_TYPE(ob) == &PyFrozenSet_Type) 72#define PyAnySet_Check(ob) \ 73 (Py_TYPE(ob) == &PySet_Type || Py_TYPE(ob) == &PyFrozenSet_Type || \ 74 PyType_IsSubtype(Py_TYPE(ob), &PySet_Type) || \ 75 PyType_IsSubtype(Py_TYPE(ob), &PyFrozenSet_Type)) 76#define PySet_Check(ob) \ 77 (Py_TYPE(ob) == &PySet_Type || \ 78 PyType_IsSubtype(Py_TYPE(ob), &PySet_Type)) 79#define PyFrozenSet_Check(ob) \ 80 (Py_TYPE(ob) == &PyFrozenSet_Type || \ 81 PyType_IsSubtype(Py_TYPE(ob), &PyFrozenSet_Type)) 82 83PyAPI_FUNC(PyObject *) PySet_New(PyObject *); 84PyAPI_FUNC(PyObject *) PyFrozenSet_New(PyObject *); 85PyAPI_FUNC(Py_ssize_t) PySet_Size(PyObject *anyset); 86#define PySet_GET_SIZE(so) (((PySetObject *)(so))->used) 87PyAPI_FUNC(int) PySet_Clear(PyObject *set); 88PyAPI_FUNC(int) PySet_Contains(PyObject *anyset, PyObject *key); 89PyAPI_FUNC(int) PySet_Discard(PyObject *set, PyObject *key); 90PyAPI_FUNC(int) PySet_Add(PyObject *set, PyObject *key); 91PyAPI_FUNC(int) _PySet_Next(PyObject *set, Py_ssize_t *pos, PyObject **key); 92PyAPI_FUNC(int) _PySet_NextEntry(PyObject *set, Py_ssize_t *pos, PyObject **key, long *hash); 93PyAPI_FUNC(PyObject *) PySet_Pop(PyObject *set); 94PyAPI_FUNC(int) _PySet_Update(PyObject *set, PyObject *iterable); 95 96#ifdef __cplusplus 97} 98#endif 99#endif /* !Py_SETOBJECT_H */ 100