blob: 92adf52c581ac7f2ae7610485b92f112265c864d [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 Hettinger9f1a6792005-07-31 01:16:36 +00009
10/*
11There are three kinds of slots in the table:
12
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*/
21
22#define PySet_MINSIZE 8
23
24typedef struct {
25 long hash; /* cached hash code for the entry key */
26 PyObject *key;
27} setentry;
28
29
Raymond Hettingera690a992003-11-16 16:17:49 +000030/*
31This data structure is shared by set and frozenset objects.
32*/
33
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000034typedef struct _setobject PySetObject;
35struct _setobject {
Raymond Hettingera690a992003-11-16 16:17:49 +000036 PyObject_HEAD
Armin Rigo89a39462004-10-28 16:32:00 +000037
Neal Norwitz0f2783c2006-06-19 05:40:44 +000038 Py_ssize_t fill; /* # Active + # Dummy */
39 Py_ssize_t used; /* # Active */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000040
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
43 * frequently needed.
Armin Rigo89a39462004-10-28 16:32:00 +000044 */
Neal Norwitz0f2783c2006-06-19 05:40:44 +000045 Py_ssize_t mask;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000046
47 /* table points to smalltable for small tables, else to
48 * additional malloc'ed memory. table is never NULL! This rule
Raymond Hettingerd7946662005-08-01 21:39:29 +000049 * saves repeated runtime null-tests.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000050 */
51 setentry *table;
52 setentry *(*lookup)(PySetObject *so, PyObject *key, long hash);
53 setentry smalltable[PySet_MINSIZE];
54
55 long hash; /* only used by frozenset objects */
56 PyObject *weakreflist; /* List of weak references */
57};
Raymond Hettingera690a992003-11-16 16:17:49 +000058
59PyAPI_DATA(PyTypeObject) PySet_Type;
60PyAPI_DATA(PyTypeObject) PyFrozenSet_Type;
61
Raymond Hettingerbc841a12005-08-07 13:02:53 +000062/* Invariants for frozensets:
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000063 * data is immutable.
64 * hash is the hash of the frozenset or -1 if not computed yet.
Raymond Hettingerbc841a12005-08-07 13:02:53 +000065 * Invariants for sets:
66 * hash is -1
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000067 */
68
Christian Heimese93237d2007-12-19 02:37:44 +000069#define PyFrozenSet_CheckExact(ob) (Py_TYPE(ob) == &PyFrozenSet_Type)
Raymond Hettingerbc841a12005-08-07 13:02:53 +000070#define PyAnySet_CheckExact(ob) \
Christian Heimese93237d2007-12-19 02:37:44 +000071 (Py_TYPE(ob) == &PySet_Type || Py_TYPE(ob) == &PyFrozenSet_Type)
Raymond Hettinger50a4bb32003-11-17 16:42:33 +000072#define PyAnySet_Check(ob) \
Christian Heimese93237d2007-12-19 02:37:44 +000073 (Py_TYPE(ob) == &PySet_Type || Py_TYPE(ob) == &PyFrozenSet_Type || \
74 PyType_IsSubtype(Py_TYPE(ob), &PySet_Type) || \
75 PyType_IsSubtype(Py_TYPE(ob), &PyFrozenSet_Type))
Raymond Hettinger7759a0c2008-01-28 21:47:42 +000076#define PySet_Check(ob) \
Amaury Forgeot d'Arccab3d982008-02-03 22:51:43 +000077 (Py_TYPE(ob) == &PySet_Type || \
78 PyType_IsSubtype(Py_TYPE(ob), &PySet_Type))
79#define PyFrozenSet_Check(ob) \
Amaury Forgeot d'Arc58e185a2008-02-03 23:14:32 +000080 (Py_TYPE(ob) == &PyFrozenSet_Type || \
Amaury Forgeot d'Arccab3d982008-02-03 22:51:43 +000081 PyType_IsSubtype(Py_TYPE(ob), &PyFrozenSet_Type))
Raymond Hettinger50a4bb32003-11-17 16:42:33 +000082
Raymond Hettingerbeb31012005-08-16 03:47:52 +000083PyAPI_FUNC(PyObject *) PySet_New(PyObject *);
84PyAPI_FUNC(PyObject *) PyFrozenSet_New(PyObject *);
Neal Norwitz8c49c822006-03-04 18:41:19 +000085PyAPI_FUNC(Py_ssize_t) PySet_Size(PyObject *anyset);
Raymond Hettingerbeb31012005-08-16 03:47:52 +000086#define PySet_GET_SIZE(so) (((PySetObject *)(so))->used)
Barry Warsaw176014f2006-03-30 22:45:35 +000087PyAPI_FUNC(int) PySet_Clear(PyObject *set);
Raymond Hettingerbeb31012005-08-16 03:47:52 +000088PyAPI_FUNC(int) PySet_Contains(PyObject *anyset, PyObject *key);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +000089PyAPI_FUNC(int) PySet_Discard(PyObject *set, PyObject *key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +000090PyAPI_FUNC(int) PySet_Add(PyObject *set, PyObject *key);
Raymond Hettinger0bbbfc42007-03-20 21:27:24 +000091PyAPI_FUNC(int) _PySet_Next(PyObject *set, Py_ssize_t *pos, PyObject **key);
92PyAPI_FUNC(int) _PySet_NextEntry(PyObject *set, Py_ssize_t *pos, PyObject **key, long *hash);
Raymond Hettingerbeb31012005-08-16 03:47:52 +000093PyAPI_FUNC(PyObject *) PySet_Pop(PyObject *set);
Barry Warsaw176014f2006-03-30 22:45:35 +000094PyAPI_FUNC(int) _PySet_Update(PyObject *set, PyObject *iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +000095
Raymond Hettingera690a992003-11-16 16:17:49 +000096#ifdef __cplusplus
97}
98#endif
99#endif /* !Py_SETOBJECT_H */