blob: f720a82dae63ea87089c1dd96d4f829f70661016 [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
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000038 int fill; /* # Active + # Dummy */
39 int used; /* # Active */
40
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 */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000045 int mask;
46
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 Hettinger9f1a6792005-07-31 01:16:36 +000062/* Invariants for frozensets only:
63 * data is immutable.
64 * hash is the hash of the frozenset or -1 if not computed yet.
65 */
66
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +000067#define PyFrozenSet_CheckExact(ob) ((ob)->ob_type == &PyFrozenSet_Type)
Raymond Hettinger50a4bb32003-11-17 16:42:33 +000068#define PyAnySet_Check(ob) \
69 ((ob)->ob_type == &PySet_Type || (ob)->ob_type == &PyFrozenSet_Type || \
70 PyType_IsSubtype((ob)->ob_type, &PySet_Type) || \
71 PyType_IsSubtype((ob)->ob_type, &PyFrozenSet_Type))
72
Raymond Hettingera690a992003-11-16 16:17:49 +000073#ifdef __cplusplus
74}
75#endif
76#endif /* !Py_SETOBJECT_H */