1 /* Set object interface */
11 There are three kinds of slots in the table:
13 1. Unused: key == NULL
14 2. Active: key != NULL and key != dummy
15 3. Dummy: key == dummy
17 Note: .pop() abuses the hash field of an Unused or Dummy slot to
18 hold a search finger. The hash field of Unused or Dummy slots has
22 #define PySet_MINSIZE 8
25 long hash
; /* cached hash code for the entry key */
31 This data structure is shared by set and frozenset objects.
34 typedef struct _setobject PySetObject
;
38 Py_ssize_t fill
; /* # Active + # Dummy */
39 Py_ssize_t used
; /* # Active */
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
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.
52 setentry
*(*lookup
)(PySetObject
*so
, PyObject
*key
, long hash
);
53 setentry smalltable
[PySet_MINSIZE
];
55 long hash
; /* only used by frozenset objects */
56 PyObject
*weakreflist
; /* List of weak references */
59 PyAPI_DATA(PyTypeObject
) PySet_Type
;
60 PyAPI_DATA(PyTypeObject
) PyFrozenSet_Type
;
61 PyAPI_DATA(PyTypeObject
) PySetIter_Type
;
63 /* Invariants for frozensets:
65 * hash is the hash of the frozenset or -1 if not computed yet.
66 * Invariants for sets:
70 #define PyFrozenSet_CheckExact(ob) (Py_TYPE(ob) == &PyFrozenSet_Type)
71 #define PyAnySet_CheckExact(ob) \
72 (Py_TYPE(ob) == &PySet_Type || Py_TYPE(ob) == &PyFrozenSet_Type)
73 #define PyAnySet_Check(ob) \
74 (Py_TYPE(ob) == &PySet_Type || Py_TYPE(ob) == &PyFrozenSet_Type || \
75 PyType_IsSubtype(Py_TYPE(ob), &PySet_Type) || \
76 PyType_IsSubtype(Py_TYPE(ob), &PyFrozenSet_Type))
77 #define PySet_Check(ob) \
78 (Py_TYPE(ob) == &PySet_Type || \
79 PyType_IsSubtype(Py_TYPE(ob), &PySet_Type))
80 #define PyFrozenSet_Check(ob) \
81 (Py_TYPE(ob) == &PyFrozenSet_Type || \
82 PyType_IsSubtype(Py_TYPE(ob), &PyFrozenSet_Type))
84 PyAPI_FUNC(PyObject
*) PySet_New(PyObject
*);
85 PyAPI_FUNC(PyObject
*) PyFrozenSet_New(PyObject
*);
86 PyAPI_FUNC(Py_ssize_t
) PySet_Size(PyObject
*anyset
);
87 #define PySet_GET_SIZE(so) (((PySetObject *)(so))->used)
88 PyAPI_FUNC(int) PySet_Clear(PyObject
*set
);
89 PyAPI_FUNC(int) PySet_Contains(PyObject
*anyset
, PyObject
*key
);
90 PyAPI_FUNC(int) PySet_Discard(PyObject
*set
, PyObject
*key
);
91 PyAPI_FUNC(int) PySet_Add(PyObject
*set
, PyObject
*key
);
92 PyAPI_FUNC(int) _PySet_NextEntry(PyObject
*set
, Py_ssize_t
*pos
, PyObject
**key
, long *hash
);
93 PyAPI_FUNC(PyObject
*) PySet_Pop(PyObject
*set
);
94 PyAPI_FUNC(int) _PySet_Update(PyObject
*set
, PyObject
*iterable
);
99 #endif /* !Py_SETOBJECT_H */