blob: 8f8e34f254688d63d3e853bc88a74b515cb0aee2 [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
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.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000020*/
Raymond Hettinger404a45d2014-12-26 17:28:16 -080021
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000022#define PySet_MINSIZE 8
23
24typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000025 PyObject *key;
Raymond Hettinger3f063a52014-12-28 17:15:12 -080026 Py_hash_t hash; /* Cached hash code of the key */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000027} setentry;
28
Raymond Hettinger404a45d2014-12-26 17:28:16 -080029/* The SetObject data structure is shared by set and frozenset objects.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000030
Raymond Hettinger404a45d2014-12-26 17:28:16 -080031Invariant for sets:
32 - hash is -1
33
34Invariants for frozensets:
35 - data is immutable.
36 - hash is the hash of the frozenset or -1 if not computed yet.
37
Raymond Hettingera690a992003-11-16 16:17:49 +000038*/
39
Raymond Hettinger404a45d2014-12-26 17:28:16 -080040typedef struct _setobject {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000041 PyObject_HEAD
Armin Rigo89a39462004-10-28 16:32:00 +000042
Raymond Hettinger404a45d2014-12-26 17:28:16 -080043 Py_ssize_t fill; /* Number active and dummy entries*/
44 Py_ssize_t used; /* Number active entries */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000045
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000046 /* The table contains mask + 1 slots, and that's a power of 2.
47 * We store the mask instead of the size because the mask is more
48 * frequently needed.
49 */
50 Py_ssize_t mask;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000051
Raymond Hettinger404a45d2014-12-26 17:28:16 -080052 /* The table points to a fixed-size smalltable for small tables
53 * or to additional malloc'ed memory for bigger tables.
54 * The table pointer is never NULL which saves us from repeated
55 * runtime null-tests.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000056 */
57 setentry *table;
Raymond Hettinger404a45d2014-12-26 17:28:16 -080058 setentry *(*lookup)(struct _setobject *so, PyObject *key, Py_hash_t hash);
59 Py_hash_t hash; /* Only used by frozenset objects */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000060 setentry smalltable[PySet_MINSIZE];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000061
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000062 PyObject *weakreflist; /* List of weak references */
Raymond Hettinger404a45d2014-12-26 17:28:16 -080063} PySetObject;
64
65#define PySet_GET_SIZE(so) (((PySetObject *)(so))->used)
66
67PyAPI_DATA(PyObject *) _PySet_Dummy;
68
69PyAPI_FUNC(int) _PySet_NextEntry(PyObject *set, Py_ssize_t *pos, PyObject **key, Py_hash_t *hash);
70PyAPI_FUNC(int) _PySet_Update(PyObject *set, PyObject *iterable);
71PyAPI_FUNC(int) PySet_ClearFreeList(void);
72
73#endif /* Section excluded by Py_LIMITED_API */
Raymond Hettingera690a992003-11-16 16:17:49 +000074
75PyAPI_DATA(PyTypeObject) PySet_Type;
76PyAPI_DATA(PyTypeObject) PyFrozenSet_Type;
Christian Heimesa22e8bd2007-11-29 22:35:39 +000077PyAPI_DATA(PyTypeObject) PySetIter_Type;
Antoine Pitrou9d952542013-08-24 21:07:07 +020078
Raymond Hettinger404a45d2014-12-26 17:28:16 -080079PyAPI_FUNC(PyObject *) PySet_New(PyObject *);
80PyAPI_FUNC(PyObject *) PyFrozenSet_New(PyObject *);
Raymond Hettingera690a992003-11-16 16:17:49 +000081
Raymond Hettinger404a45d2014-12-26 17:28:16 -080082PyAPI_FUNC(int) PySet_Add(PyObject *set, PyObject *key);
83PyAPI_FUNC(int) PySet_Clear(PyObject *set);
84PyAPI_FUNC(int) PySet_Contains(PyObject *anyset, PyObject *key);
85PyAPI_FUNC(int) PySet_Discard(PyObject *set, PyObject *key);
86PyAPI_FUNC(PyObject *) PySet_Pop(PyObject *set);
87PyAPI_FUNC(Py_ssize_t) PySet_Size(PyObject *anyset);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000088
Christian Heimes90aa7642007-12-19 02:45:37 +000089#define PyFrozenSet_CheckExact(ob) (Py_TYPE(ob) == &PyFrozenSet_Type)
Raymond Hettingerbc841a12005-08-07 13:02:53 +000090#define PyAnySet_CheckExact(ob) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000091 (Py_TYPE(ob) == &PySet_Type || Py_TYPE(ob) == &PyFrozenSet_Type)
Raymond Hettinger50a4bb32003-11-17 16:42:33 +000092#define PyAnySet_Check(ob) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000093 (Py_TYPE(ob) == &PySet_Type || Py_TYPE(ob) == &PyFrozenSet_Type || \
94 PyType_IsSubtype(Py_TYPE(ob), &PySet_Type) || \
95 PyType_IsSubtype(Py_TYPE(ob), &PyFrozenSet_Type))
Christian Heimesfd66e512008-01-29 12:18:50 +000096#define PySet_Check(ob) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000097 (Py_TYPE(ob) == &PySet_Type || \
98 PyType_IsSubtype(Py_TYPE(ob), &PySet_Type))
Christian Heimes15ebc882008-02-04 18:48:49 +000099#define PyFrozenSet_Check(ob) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000100 (Py_TYPE(ob) == &PyFrozenSet_Type || \
101 PyType_IsSubtype(Py_TYPE(ob), &PyFrozenSet_Type))
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000102
Raymond Hettingera690a992003-11-16 16:17:49 +0000103#ifdef __cplusplus
104}
105#endif
106#endif /* !Py_SETOBJECT_H */