Chris@87
|
1 /* Set object interface */
|
Chris@87
|
2
|
Chris@87
|
3 #ifndef Py_SETOBJECT_H
|
Chris@87
|
4 #define Py_SETOBJECT_H
|
Chris@87
|
5 #ifdef __cplusplus
|
Chris@87
|
6 extern "C" {
|
Chris@87
|
7 #endif
|
Chris@87
|
8
|
Chris@87
|
9
|
Chris@87
|
10 /*
|
Chris@87
|
11 There are three kinds of slots in the table:
|
Chris@87
|
12
|
Chris@87
|
13 1. Unused: key == NULL
|
Chris@87
|
14 2. Active: key != NULL and key != dummy
|
Chris@87
|
15 3. Dummy: key == dummy
|
Chris@87
|
16
|
Chris@87
|
17 Note: .pop() abuses the hash field of an Unused or Dummy slot to
|
Chris@87
|
18 hold a search finger. The hash field of Unused or Dummy slots has
|
Chris@87
|
19 no meaning otherwise.
|
Chris@87
|
20 */
|
Chris@87
|
21
|
Chris@87
|
22 #define PySet_MINSIZE 8
|
Chris@87
|
23
|
Chris@87
|
24 typedef struct {
|
Chris@87
|
25 long hash; /* cached hash code for the entry key */
|
Chris@87
|
26 PyObject *key;
|
Chris@87
|
27 } setentry;
|
Chris@87
|
28
|
Chris@87
|
29
|
Chris@87
|
30 /*
|
Chris@87
|
31 This data structure is shared by set and frozenset objects.
|
Chris@87
|
32 */
|
Chris@87
|
33
|
Chris@87
|
34 typedef struct _setobject PySetObject;
|
Chris@87
|
35 struct _setobject {
|
Chris@87
|
36 PyObject_HEAD
|
Chris@87
|
37
|
Chris@87
|
38 Py_ssize_t fill; /* # Active + # Dummy */
|
Chris@87
|
39 Py_ssize_t used; /* # Active */
|
Chris@87
|
40
|
Chris@87
|
41 /* The table contains mask + 1 slots, and that's a power of 2.
|
Chris@87
|
42 * We store the mask instead of the size because the mask is more
|
Chris@87
|
43 * frequently needed.
|
Chris@87
|
44 */
|
Chris@87
|
45 Py_ssize_t mask;
|
Chris@87
|
46
|
Chris@87
|
47 /* table points to smalltable for small tables, else to
|
Chris@87
|
48 * additional malloc'ed memory. table is never NULL! This rule
|
Chris@87
|
49 * saves repeated runtime null-tests.
|
Chris@87
|
50 */
|
Chris@87
|
51 setentry *table;
|
Chris@87
|
52 setentry *(*lookup)(PySetObject *so, PyObject *key, long hash);
|
Chris@87
|
53 setentry smalltable[PySet_MINSIZE];
|
Chris@87
|
54
|
Chris@87
|
55 long hash; /* only used by frozenset objects */
|
Chris@87
|
56 PyObject *weakreflist; /* List of weak references */
|
Chris@87
|
57 };
|
Chris@87
|
58
|
Chris@87
|
59 PyAPI_DATA(PyTypeObject) PySet_Type;
|
Chris@87
|
60 PyAPI_DATA(PyTypeObject) PyFrozenSet_Type;
|
Chris@87
|
61
|
Chris@87
|
62 /* Invariants for frozensets:
|
Chris@87
|
63 * data is immutable.
|
Chris@87
|
64 * hash is the hash of the frozenset or -1 if not computed yet.
|
Chris@87
|
65 * Invariants for sets:
|
Chris@87
|
66 * hash is -1
|
Chris@87
|
67 */
|
Chris@87
|
68
|
Chris@87
|
69 #define PyFrozenSet_CheckExact(ob) (Py_TYPE(ob) == &PyFrozenSet_Type)
|
Chris@87
|
70 #define PyAnySet_CheckExact(ob) \
|
Chris@87
|
71 (Py_TYPE(ob) == &PySet_Type || Py_TYPE(ob) == &PyFrozenSet_Type)
|
Chris@87
|
72 #define PyAnySet_Check(ob) \
|
Chris@87
|
73 (Py_TYPE(ob) == &PySet_Type || Py_TYPE(ob) == &PyFrozenSet_Type || \
|
Chris@87
|
74 PyType_IsSubtype(Py_TYPE(ob), &PySet_Type) || \
|
Chris@87
|
75 PyType_IsSubtype(Py_TYPE(ob), &PyFrozenSet_Type))
|
Chris@87
|
76 #define PySet_Check(ob) \
|
Chris@87
|
77 (Py_TYPE(ob) == &PySet_Type || \
|
Chris@87
|
78 PyType_IsSubtype(Py_TYPE(ob), &PySet_Type))
|
Chris@87
|
79 #define PyFrozenSet_Check(ob) \
|
Chris@87
|
80 (Py_TYPE(ob) == &PyFrozenSet_Type || \
|
Chris@87
|
81 PyType_IsSubtype(Py_TYPE(ob), &PyFrozenSet_Type))
|
Chris@87
|
82
|
Chris@87
|
83 PyAPI_FUNC(PyObject *) PySet_New(PyObject *);
|
Chris@87
|
84 PyAPI_FUNC(PyObject *) PyFrozenSet_New(PyObject *);
|
Chris@87
|
85 PyAPI_FUNC(Py_ssize_t) PySet_Size(PyObject *anyset);
|
Chris@87
|
86 #define PySet_GET_SIZE(so) (((PySetObject *)(so))->used)
|
Chris@87
|
87 PyAPI_FUNC(int) PySet_Clear(PyObject *set);
|
Chris@87
|
88 PyAPI_FUNC(int) PySet_Contains(PyObject *anyset, PyObject *key);
|
Chris@87
|
89 PyAPI_FUNC(int) PySet_Discard(PyObject *set, PyObject *key);
|
Chris@87
|
90 PyAPI_FUNC(int) PySet_Add(PyObject *set, PyObject *key);
|
Chris@87
|
91 PyAPI_FUNC(int) _PySet_Next(PyObject *set, Py_ssize_t *pos, PyObject **key);
|
Chris@87
|
92 PyAPI_FUNC(int) _PySet_NextEntry(PyObject *set, Py_ssize_t *pos, PyObject **key, long *hash);
|
Chris@87
|
93 PyAPI_FUNC(PyObject *) PySet_Pop(PyObject *set);
|
Chris@87
|
94 PyAPI_FUNC(int) _PySet_Update(PyObject *set, PyObject *iterable);
|
Chris@87
|
95
|
Chris@87
|
96 #ifdef __cplusplus
|
Chris@87
|
97 }
|
Chris@87
|
98 #endif
|
Chris@87
|
99 #endif /* !Py_SETOBJECT_H */
|