setobject.h 3.22 KB
Newer Older
1 2 3 4 5 6 7 8
/* Set object interface */

#ifndef Py_SETOBJECT_H
#define Py_SETOBJECT_H
#ifdef __cplusplus
extern "C" {
#endif

9

10
/*
11 12 13 14 15
There are three kinds of slots in the table:

1. Unused:  key == NULL
2. Active:  key != NULL and key != dummy
3. Dummy:   key == dummy
16 17 18 19

Note: .pop() abuses the hash field of an Unused or Dummy slot to
hold a search finger.  The hash field of Unused or Dummy slots has
no meaning otherwise.
20
*/
21
#ifndef Py_LIMITED_API
22 23
#define PySet_MINSIZE 8

24
typedef struct {
25
    /* Cached hash code of the key. */
26
    PyObject *key;
27
    Py_hash_t hash;
28 29 30 31 32 33 34 35 36
} setentry;


/*
This data structure is shared by set and frozenset objects.
*/

typedef struct _setobject PySetObject;
struct _setobject {
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
    PyObject_HEAD

    Py_ssize_t fill;  /* # Active + # Dummy */
    Py_ssize_t used;  /* # Active */

    /* The table contains mask + 1 slots, and that's a power of 2.
     * We store the mask instead of the size because the mask is more
     * frequently needed.
     */
    Py_ssize_t mask;

    /* table points to smalltable for small tables, else to
     * additional malloc'ed memory.  table is never NULL!  This rule
     * saves repeated runtime null-tests.
     */
    setentry *table;
53
    setentry *(*lookup)(PySetObject *so, PyObject *key, Py_hash_t hash);
54
    Py_hash_t hash;             /* only used by frozenset objects */
55 56 57
    setentry smalltable[PySet_MINSIZE];

    PyObject *weakreflist;      /* List of weak references */
58
};
59
#endif /* Py_LIMITED_API */
60 61 62

PyAPI_DATA(PyTypeObject) PySet_Type;
PyAPI_DATA(PyTypeObject) PyFrozenSet_Type;
63
PyAPI_DATA(PyTypeObject) PySetIter_Type;
64 65 66 67
#ifndef Py_LIMITED_API
PyAPI_DATA(PyObject *) _PySet_Dummy;
#endif

68

69
/* Invariants for frozensets:
70 71
 *     data is immutable.
 *     hash is the hash of the frozenset or -1 if not computed yet.
72 73
 * Invariants for sets:
 *     hash is -1
74 75
 */

76
#define PyFrozenSet_CheckExact(ob) (Py_TYPE(ob) == &PyFrozenSet_Type)
77
#define PyAnySet_CheckExact(ob) \
78
    (Py_TYPE(ob) == &PySet_Type || Py_TYPE(ob) == &PyFrozenSet_Type)
79
#define PyAnySet_Check(ob) \
80 81 82
    (Py_TYPE(ob) == &PySet_Type || Py_TYPE(ob) == &PyFrozenSet_Type || \
      PyType_IsSubtype(Py_TYPE(ob), &PySet_Type) || \
      PyType_IsSubtype(Py_TYPE(ob), &PyFrozenSet_Type))
83
#define PySet_Check(ob) \
84 85
    (Py_TYPE(ob) == &PySet_Type || \
    PyType_IsSubtype(Py_TYPE(ob), &PySet_Type))
Christian Heimes's avatar
Christian Heimes committed
86
#define   PyFrozenSet_Check(ob) \
87 88
    (Py_TYPE(ob) == &PyFrozenSet_Type || \
      PyType_IsSubtype(Py_TYPE(ob), &PyFrozenSet_Type))
89

90 91
PyAPI_FUNC(PyObject *) PySet_New(PyObject *);
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

110 111 112 113
#ifdef __cplusplus
}
#endif
#endif /* !Py_SETOBJECT_H */