blob: a8b8d33ebb66e951dfff2298ab3f3834f5f27c91 [file] [log] [blame]
Raymond Hettingera690a992003-11-16 16:17:49 +00001/* Set object interface */
2
3#ifndef Py_SETOBJECT_H
4#define Py_SETOBJECT_H
5#ifdef __cplusplus
6extern "C" {
7#endif
8
Raymond Hettinger404a45d2014-12-26 17:28:16 -08009#ifndef Py_LIMITED_API
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000010
Raymond Hettinger404a45d2014-12-26 17:28:16 -080011/* There are three kinds of entries in the table:
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000012
131. Unused: key == NULL
142. Active: key != NULL and key != dummy
153. Dummy: key == dummy
Raymond Hettinger67962ab2005-08-02 03:45:16 +000016
Raymond Hettinger1202a472015-01-18 13:12:42 -080017The hash field of Unused or Dummy slots have no meaning.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000018*/
Raymond Hettinger404a45d2014-12-26 17:28:16 -080019
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000020#define PySet_MINSIZE 8
21
22typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000023 PyObject *key;
Raymond Hettinger3f063a52014-12-28 17:15:12 -080024 Py_hash_t hash; /* Cached hash code of the key */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000025} setentry;
26
Raymond Hettinger404a45d2014-12-26 17:28:16 -080027/* The SetObject data structure is shared by set and frozenset objects.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000028
Raymond Hettinger404a45d2014-12-26 17:28:16 -080029Invariant for sets:
30 - hash is -1
31
32Invariants for frozensets:
33 - data is immutable.
34 - hash is the hash of the frozenset or -1 if not computed yet.
35
Raymond Hettingera690a992003-11-16 16:17:49 +000036*/
37
Raymond Hettinger93035c42015-01-25 16:12:49 -080038typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000039 PyObject_HEAD
Armin Rigo89a39462004-10-28 16:32:00 +000040
Raymond Hettinger404a45d2014-12-26 17:28:16 -080041 Py_ssize_t fill; /* Number active and dummy entries*/
42 Py_ssize_t used; /* Number active entries */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000043
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000044 /* The table contains mask + 1 slots, and that's a power of 2.
45 * We store the mask instead of the size because the mask is more
46 * frequently needed.
47 */
48 Py_ssize_t mask;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000049
Raymond Hettinger404a45d2014-12-26 17:28:16 -080050 /* The table points to a fixed-size smalltable for small tables
51 * or to additional malloc'ed memory for bigger tables.
52 * The table pointer is never NULL which saves us from repeated
53 * runtime null-tests.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000054 */
55 setentry *table;
Raymond Hettinger404a45d2014-12-26 17:28:16 -080056 Py_hash_t hash; /* Only used by frozenset objects */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000057 setentry smalltable[PySet_MINSIZE];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000058
Raymond Hettinger1202a472015-01-18 13:12:42 -080059 Py_ssize_t finger; /* Search finger for pop() */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000060 PyObject *weakreflist; /* List of weak references */
Raymond Hettinger404a45d2014-12-26 17:28:16 -080061} PySetObject;
62
63#define PySet_GET_SIZE(so) (((PySetObject *)(so))->used)
64
65PyAPI_DATA(PyObject *) _PySet_Dummy;
66
67PyAPI_FUNC(int) _PySet_NextEntry(PyObject *set, Py_ssize_t *pos, PyObject **key, Py_hash_t *hash);
68PyAPI_FUNC(int) _PySet_Update(PyObject *set, PyObject *iterable);
69PyAPI_FUNC(int) PySet_ClearFreeList(void);
70
71#endif /* Section excluded by Py_LIMITED_API */
Raymond Hettingera690a992003-11-16 16:17:49 +000072
73PyAPI_DATA(PyTypeObject) PySet_Type;
74PyAPI_DATA(PyTypeObject) PyFrozenSet_Type;
Christian Heimesa22e8bd2007-11-29 22:35:39 +000075PyAPI_DATA(PyTypeObject) PySetIter_Type;
Antoine Pitrou9d952542013-08-24 21:07:07 +020076
Raymond Hettinger404a45d2014-12-26 17:28:16 -080077PyAPI_FUNC(PyObject *) PySet_New(PyObject *);
78PyAPI_FUNC(PyObject *) PyFrozenSet_New(PyObject *);
Raymond Hettingera690a992003-11-16 16:17:49 +000079
Raymond Hettinger404a45d2014-12-26 17:28:16 -080080PyAPI_FUNC(int) PySet_Add(PyObject *set, PyObject *key);
81PyAPI_FUNC(int) PySet_Clear(PyObject *set);
82PyAPI_FUNC(int) PySet_Contains(PyObject *anyset, PyObject *key);
83PyAPI_FUNC(int) PySet_Discard(PyObject *set, PyObject *key);
84PyAPI_FUNC(PyObject *) PySet_Pop(PyObject *set);
85PyAPI_FUNC(Py_ssize_t) PySet_Size(PyObject *anyset);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000086
Christian Heimes90aa7642007-12-19 02:45:37 +000087#define PyFrozenSet_CheckExact(ob) (Py_TYPE(ob) == &PyFrozenSet_Type)
Raymond Hettingerbc841a12005-08-07 13:02:53 +000088#define PyAnySet_CheckExact(ob) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000089 (Py_TYPE(ob) == &PySet_Type || Py_TYPE(ob) == &PyFrozenSet_Type)
Raymond Hettinger50a4bb32003-11-17 16:42:33 +000090#define PyAnySet_Check(ob) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000091 (Py_TYPE(ob) == &PySet_Type || Py_TYPE(ob) == &PyFrozenSet_Type || \
92 PyType_IsSubtype(Py_TYPE(ob), &PySet_Type) || \
93 PyType_IsSubtype(Py_TYPE(ob), &PyFrozenSet_Type))
Christian Heimesfd66e512008-01-29 12:18:50 +000094#define PySet_Check(ob) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000095 (Py_TYPE(ob) == &PySet_Type || \
96 PyType_IsSubtype(Py_TYPE(ob), &PySet_Type))
Christian Heimes15ebc882008-02-04 18:48:49 +000097#define PyFrozenSet_Check(ob) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000098 (Py_TYPE(ob) == &PyFrozenSet_Type || \
99 PyType_IsSubtype(Py_TYPE(ob), &PyFrozenSet_Type))
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000100
Raymond Hettingera690a992003-11-16 16:17:49 +0000101#ifdef __cplusplus
102}
103#endif
104#endif /* !Py_SETOBJECT_H */