blob: f17bc1b035457f874a8c68f2d82eb2b46888ca75 [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 Hettingera5ebbf62015-01-26 21:54:35 -080017The hash field of Unused slots have no meaning.
18The hash field of Dummny slots are set to -1
19meaning that dummy entries can be detected by
20either entry->key==dummy or by entry->hash==-1.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000021*/
Raymond Hettinger404a45d2014-12-26 17:28:16 -080022
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000023#define PySet_MINSIZE 8
24
25typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000026 PyObject *key;
Raymond Hettinger3f063a52014-12-28 17:15:12 -080027 Py_hash_t hash; /* Cached hash code of the key */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000028} setentry;
29
Raymond Hettinger404a45d2014-12-26 17:28:16 -080030/* The SetObject data structure is shared by set and frozenset objects.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000031
Raymond Hettinger404a45d2014-12-26 17:28:16 -080032Invariant for sets:
33 - hash is -1
34
35Invariants for frozensets:
36 - data is immutable.
37 - hash is the hash of the frozenset or -1 if not computed yet.
38
Raymond Hettingera690a992003-11-16 16:17:49 +000039*/
40
Raymond Hettinger93035c42015-01-25 16:12:49 -080041typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000042 PyObject_HEAD
Armin Rigo89a39462004-10-28 16:32:00 +000043
Raymond Hettinger404a45d2014-12-26 17:28:16 -080044 Py_ssize_t fill; /* Number active and dummy entries*/
45 Py_ssize_t used; /* Number active entries */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000046
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000047 /* The table contains mask + 1 slots, and that's a power of 2.
48 * We store the mask instead of the size because the mask is more
49 * frequently needed.
50 */
51 Py_ssize_t mask;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000052
Raymond Hettinger404a45d2014-12-26 17:28:16 -080053 /* The table points to a fixed-size smalltable for small tables
54 * or to additional malloc'ed memory for bigger tables.
55 * The table pointer is never NULL which saves us from repeated
56 * runtime null-tests.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000057 */
58 setentry *table;
Raymond Hettinger404a45d2014-12-26 17:28:16 -080059 Py_hash_t hash; /* Only used by frozenset objects */
Raymond Hettinger1202a472015-01-18 13:12:42 -080060 Py_ssize_t finger; /* Search finger for pop() */
Raymond Hettingerc5644122015-01-29 22:00:32 -080061
62 setentry smalltable[PySet_MINSIZE];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000063 PyObject *weakreflist; /* List of weak references */
Raymond Hettinger404a45d2014-12-26 17:28:16 -080064} PySetObject;
65
66#define PySet_GET_SIZE(so) (((PySetObject *)(so))->used)
67
68PyAPI_DATA(PyObject *) _PySet_Dummy;
69
70PyAPI_FUNC(int) _PySet_NextEntry(PyObject *set, Py_ssize_t *pos, PyObject **key, Py_hash_t *hash);
71PyAPI_FUNC(int) _PySet_Update(PyObject *set, PyObject *iterable);
72PyAPI_FUNC(int) PySet_ClearFreeList(void);
73
74#endif /* Section excluded by Py_LIMITED_API */
Raymond Hettingera690a992003-11-16 16:17:49 +000075
76PyAPI_DATA(PyTypeObject) PySet_Type;
77PyAPI_DATA(PyTypeObject) PyFrozenSet_Type;
Christian Heimesa22e8bd2007-11-29 22:35:39 +000078PyAPI_DATA(PyTypeObject) PySetIter_Type;
Antoine Pitrou9d952542013-08-24 21:07:07 +020079
Raymond Hettinger404a45d2014-12-26 17:28:16 -080080PyAPI_FUNC(PyObject *) PySet_New(PyObject *);
81PyAPI_FUNC(PyObject *) PyFrozenSet_New(PyObject *);
Raymond Hettingera690a992003-11-16 16:17:49 +000082
Raymond Hettinger404a45d2014-12-26 17:28:16 -080083PyAPI_FUNC(int) PySet_Add(PyObject *set, PyObject *key);
84PyAPI_FUNC(int) PySet_Clear(PyObject *set);
85PyAPI_FUNC(int) PySet_Contains(PyObject *anyset, PyObject *key);
86PyAPI_FUNC(int) PySet_Discard(PyObject *set, PyObject *key);
87PyAPI_FUNC(PyObject *) PySet_Pop(PyObject *set);
88PyAPI_FUNC(Py_ssize_t) PySet_Size(PyObject *anyset);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000089
Christian Heimes90aa7642007-12-19 02:45:37 +000090#define PyFrozenSet_CheckExact(ob) (Py_TYPE(ob) == &PyFrozenSet_Type)
Raymond Hettingerbc841a12005-08-07 13:02:53 +000091#define PyAnySet_CheckExact(ob) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000092 (Py_TYPE(ob) == &PySet_Type || Py_TYPE(ob) == &PyFrozenSet_Type)
Raymond Hettinger50a4bb32003-11-17 16:42:33 +000093#define PyAnySet_Check(ob) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000094 (Py_TYPE(ob) == &PySet_Type || Py_TYPE(ob) == &PyFrozenSet_Type || \
95 PyType_IsSubtype(Py_TYPE(ob), &PySet_Type) || \
96 PyType_IsSubtype(Py_TYPE(ob), &PyFrozenSet_Type))
Christian Heimesfd66e512008-01-29 12:18:50 +000097#define PySet_Check(ob) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000098 (Py_TYPE(ob) == &PySet_Type || \
99 PyType_IsSubtype(Py_TYPE(ob), &PySet_Type))
Christian Heimes15ebc882008-02-04 18:48:49 +0000100#define PyFrozenSet_Check(ob) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000101 (Py_TYPE(ob) == &PyFrozenSet_Type || \
102 PyType_IsSubtype(Py_TYPE(ob), &PyFrozenSet_Type))
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000103
Raymond Hettingera690a992003-11-16 16:17:49 +0000104#ifdef __cplusplus
105}
106#endif
107#endif /* !Py_SETOBJECT_H */