|
1 | | - |
2 | 1 | /* Set object interface */ |
3 | 2 |
|
4 | 3 | #ifndef Py_SETOBJECT_H |
|
7 | 6 | extern "C" { |
8 | 7 | #endif |
9 | 8 |
|
| 9 | + |
10 | 10 | /* |
11 | | -This data structure is shared by set and frozenset objects. |
| 11 | +There are three kinds of slots in the table: |
| 12 | +
|
| 13 | +1. Unused: key == NULL |
| 14 | +2. Active: key != NULL and key != dummy |
| 15 | +3. Dummy: key == dummy |
12 | 16 | */ |
13 | 17 |
|
| 18 | +#define PySet_MINSIZE 8 |
| 19 | + |
14 | 20 | typedef struct { |
| 21 | + long hash; /* cached hash code for the entry key */ |
| 22 | + PyObject *key; |
| 23 | +} setentry; |
| 24 | + |
| 25 | + |
| 26 | +/* |
| 27 | +This data structure is shared by set and frozenset objects. |
| 28 | +*/ |
| 29 | + |
| 30 | +typedef struct _setobject PySetObject; |
| 31 | +struct _setobject { |
15 | 32 | PyObject_HEAD |
16 | | - PyObject *data; |
17 | | - long hash; /* only used by frozenset objects */ |
18 | | - PyObject *weakreflist; /* List of weak references */ |
19 | | - |
20 | | - /* Invariants: |
21 | | - * data is a dictionary whose values are all True. |
22 | | - * data points to the same dict for the whole life of the set. |
23 | | - * For frozensets only: |
24 | | - * data is immutable. |
25 | | - * hash is the hash of the frozenset or -1 if not computed yet. |
| 33 | + |
| 34 | + int fill; /* # Active + # Dummy */ |
| 35 | + int used; /* # Active */ |
| 36 | + |
| 37 | + /* The table contains mask + 1 slots, and that's a power of 2. |
| 38 | + * We store the mask instead of the size because the mask is more |
| 39 | + * frequently needed. |
26 | 40 | */ |
27 | | -} PySetObject; |
| 41 | + int mask; |
| 42 | + |
| 43 | + /* table points to smalltable for small tables, else to |
| 44 | + * additional malloc'ed memory. table is never NULL! This rule |
| 45 | + * saves repeated runtime null-tests in the workhorse getitem and |
| 46 | + * setitem calls. |
| 47 | + */ |
| 48 | + setentry *table; |
| 49 | + setentry *(*lookup)(PySetObject *so, PyObject *key, long hash); |
| 50 | + setentry smalltable[PySet_MINSIZE]; |
| 51 | + |
| 52 | + long hash; /* only used by frozenset objects */ |
| 53 | + PyObject *weakreflist; /* List of weak references */ |
| 54 | +}; |
28 | 55 |
|
29 | 56 | PyAPI_DATA(PyTypeObject) PySet_Type; |
30 | 57 | PyAPI_DATA(PyTypeObject) PyFrozenSet_Type; |
31 | 58 |
|
| 59 | +/* Invariants for frozensets only: |
| 60 | + * data is immutable. |
| 61 | + * hash is the hash of the frozenset or -1 if not computed yet. |
| 62 | + */ |
| 63 | + |
32 | 64 | #define PyFrozenSet_CheckExact(ob) ((ob)->ob_type == &PyFrozenSet_Type) |
33 | 65 | #define PyAnySet_Check(ob) \ |
34 | 66 | ((ob)->ob_type == &PySet_Type || (ob)->ob_type == &PyFrozenSet_Type || \ |
|
0 commit comments