blob: 87ec1c8afc00c4b8262f5327f735960c1c1d939b [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
Raymond Hettinger9344bd82015-08-01 15:21:41 -0700131. Unused: key == NULL and hash == 0
142. Dummy: key == dummy and hash == -1
153. Active: key != NULL and key != dummy and hash != -1
Raymond Hettinger67962ab2005-08-02 03:45:16 +000016
Raymond Hettinger9344bd82015-08-01 15:21:41 -070017The hash field of Unused slots is always zero.
18
19The hash field of Dummy slots are set to -1
Raymond Hettingera5ebbf62015-01-26 21:54:35 -080020meaning that dummy entries can be detected by
21either entry->key==dummy or by entry->hash==-1.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000022*/
Raymond Hettinger404a45d2014-12-26 17:28:16 -080023
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000024#define PySet_MINSIZE 8
25
26typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000027 PyObject *key;
Raymond Hettinger3f063a52014-12-28 17:15:12 -080028 Py_hash_t hash; /* Cached hash code of the key */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000029} setentry;
30
Raymond Hettinger404a45d2014-12-26 17:28:16 -080031/* The SetObject data structure is shared by set and frozenset objects.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000032
Raymond Hettinger404a45d2014-12-26 17:28:16 -080033Invariant for sets:
34 - hash is -1
35
36Invariants for frozensets:
37 - data is immutable.
38 - hash is the hash of the frozenset or -1 if not computed yet.
39
Raymond Hettingera690a992003-11-16 16:17:49 +000040*/
41
Raymond Hettinger93035c42015-01-25 16:12:49 -080042typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000043 PyObject_HEAD
Armin Rigo89a39462004-10-28 16:32:00 +000044
Raymond Hettinger404a45d2014-12-26 17:28:16 -080045 Py_ssize_t fill; /* Number active and dummy entries*/
46 Py_ssize_t used; /* Number active entries */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000047
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000048 /* The table contains mask + 1 slots, and that's a power of 2.
49 * We store the mask instead of the size because the mask is more
50 * frequently needed.
51 */
52 Py_ssize_t mask;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000053
Raymond Hettinger404a45d2014-12-26 17:28:16 -080054 /* The table points to a fixed-size smalltable for small tables
55 * or to additional malloc'ed memory for bigger tables.
56 * The table pointer is never NULL which saves us from repeated
57 * runtime null-tests.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000058 */
59 setentry *table;
Raymond Hettinger404a45d2014-12-26 17:28:16 -080060 Py_hash_t hash; /* Only used by frozenset objects */
Raymond Hettinger1202a472015-01-18 13:12:42 -080061 Py_ssize_t finger; /* Search finger for pop() */
Raymond Hettingerc5644122015-01-29 22:00:32 -080062
63 setentry smalltable[PySet_MINSIZE];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000064 PyObject *weakreflist; /* List of weak references */
Raymond Hettinger404a45d2014-12-26 17:28:16 -080065} PySetObject;
66
67#define PySet_GET_SIZE(so) (((PySetObject *)(so))->used)
68
69PyAPI_DATA(PyObject *) _PySet_Dummy;
70
71PyAPI_FUNC(int) _PySet_NextEntry(PyObject *set, Py_ssize_t *pos, PyObject **key, Py_hash_t *hash);
72PyAPI_FUNC(int) _PySet_Update(PyObject *set, PyObject *iterable);
73PyAPI_FUNC(int) PySet_ClearFreeList(void);
74
75#endif /* Section excluded by Py_LIMITED_API */
Raymond Hettingera690a992003-11-16 16:17:49 +000076
77PyAPI_DATA(PyTypeObject) PySet_Type;
78PyAPI_DATA(PyTypeObject) PyFrozenSet_Type;
Christian Heimesa22e8bd2007-11-29 22:35:39 +000079PyAPI_DATA(PyTypeObject) PySetIter_Type;
Antoine Pitrou9d952542013-08-24 21:07:07 +020080
Raymond Hettinger404a45d2014-12-26 17:28:16 -080081PyAPI_FUNC(PyObject *) PySet_New(PyObject *);
82PyAPI_FUNC(PyObject *) PyFrozenSet_New(PyObject *);
Raymond Hettingera690a992003-11-16 16:17:49 +000083
Raymond Hettinger404a45d2014-12-26 17:28:16 -080084PyAPI_FUNC(int) PySet_Add(PyObject *set, PyObject *key);
85PyAPI_FUNC(int) PySet_Clear(PyObject *set);
86PyAPI_FUNC(int) PySet_Contains(PyObject *anyset, PyObject *key);
87PyAPI_FUNC(int) PySet_Discard(PyObject *set, PyObject *key);
88PyAPI_FUNC(PyObject *) PySet_Pop(PyObject *set);
89PyAPI_FUNC(Py_ssize_t) PySet_Size(PyObject *anyset);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000090
Christian Heimes90aa7642007-12-19 02:45:37 +000091#define PyFrozenSet_CheckExact(ob) (Py_TYPE(ob) == &PyFrozenSet_Type)
Raymond Hettingerbc841a12005-08-07 13:02:53 +000092#define PyAnySet_CheckExact(ob) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000093 (Py_TYPE(ob) == &PySet_Type || Py_TYPE(ob) == &PyFrozenSet_Type)
Raymond Hettinger50a4bb32003-11-17 16:42:33 +000094#define PyAnySet_Check(ob) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000095 (Py_TYPE(ob) == &PySet_Type || Py_TYPE(ob) == &PyFrozenSet_Type || \
96 PyType_IsSubtype(Py_TYPE(ob), &PySet_Type) || \
97 PyType_IsSubtype(Py_TYPE(ob), &PyFrozenSet_Type))
Christian Heimesfd66e512008-01-29 12:18:50 +000098#define PySet_Check(ob) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000099 (Py_TYPE(ob) == &PySet_Type || \
100 PyType_IsSubtype(Py_TYPE(ob), &PySet_Type))
Christian Heimes15ebc882008-02-04 18:48:49 +0000101#define PyFrozenSet_Check(ob) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000102 (Py_TYPE(ob) == &PyFrozenSet_Type || \
103 PyType_IsSubtype(Py_TYPE(ob), &PyFrozenSet_Type))
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000104
Raymond Hettingera690a992003-11-16 16:17:49 +0000105#ifdef __cplusplus
106}
107#endif
108#endif /* !Py_SETOBJECT_H */