Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1 | /* Set object interface */ |
| 2 | |
| 3 | #ifndef Py_SETOBJECT_H |
| 4 | #define Py_SETOBJECT_H |
| 5 | #ifdef __cplusplus |
| 6 | extern "C" { |
| 7 | #endif |
| 8 | |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 9 | |
| 10 | /* |
| 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 |
Raymond Hettinger | 67962ab | 2005-08-02 03:45:16 +0000 | [diff] [blame] | 16 | |
| 17 | Note: .pop() abuses the hash field of an Unused or Dummy slot to |
| 18 | hold a search finger. The hash field of Unused or Dummy slots has |
| 19 | no meaning otherwise. |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #define PySet_MINSIZE 8 |
| 23 | |
| 24 | typedef struct { |
| 25 | long hash; /* cached hash code for the entry key */ |
| 26 | PyObject *key; |
| 27 | } setentry; |
| 28 | |
| 29 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 30 | /* |
| 31 | This data structure is shared by set and frozenset objects. |
| 32 | */ |
| 33 | |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 34 | typedef struct _setobject PySetObject; |
| 35 | struct _setobject { |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 36 | PyObject_HEAD |
Armin Rigo | 89a3946 | 2004-10-28 16:32:00 +0000 | [diff] [blame] | 37 | |
Neal Norwitz | 0f2783c | 2006-06-19 05:40:44 +0000 | [diff] [blame] | 38 | Py_ssize_t fill; /* # Active + # Dummy */ |
| 39 | Py_ssize_t used; /* # Active */ |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 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 Rigo | 89a3946 | 2004-10-28 16:32:00 +0000 | [diff] [blame] | 44 | */ |
Neal Norwitz | 0f2783c | 2006-06-19 05:40:44 +0000 | [diff] [blame] | 45 | Py_ssize_t mask; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 46 | |
| 47 | /* table points to smalltable for small tables, else to |
| 48 | * additional malloc'ed memory. table is never NULL! This rule |
Raymond Hettinger | d794666 | 2005-08-01 21:39:29 +0000 | [diff] [blame] | 49 | * saves repeated runtime null-tests. |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 50 | */ |
| 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 Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 58 | |
| 59 | PyAPI_DATA(PyTypeObject) PySet_Type; |
| 60 | PyAPI_DATA(PyTypeObject) PyFrozenSet_Type; |
| 61 | |
Raymond Hettinger | bc841a1 | 2005-08-07 13:02:53 +0000 | [diff] [blame] | 62 | /* Invariants for frozensets: |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 63 | * data is immutable. |
| 64 | * hash is the hash of the frozenset or -1 if not computed yet. |
Raymond Hettinger | bc841a1 | 2005-08-07 13:02:53 +0000 | [diff] [blame] | 65 | * Invariants for sets: |
| 66 | * hash is -1 |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 67 | */ |
| 68 | |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 69 | #define PyFrozenSet_CheckExact(ob) (Py_TYPE(ob) == &PyFrozenSet_Type) |
Raymond Hettinger | bc841a1 | 2005-08-07 13:02:53 +0000 | [diff] [blame] | 70 | #define PyAnySet_CheckExact(ob) \ |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 71 | (Py_TYPE(ob) == &PySet_Type || Py_TYPE(ob) == &PyFrozenSet_Type) |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 72 | #define PyAnySet_Check(ob) \ |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 73 | (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 Hettinger | 7759a0c | 2008-01-28 21:47:42 +0000 | [diff] [blame] | 76 | #define PySet_Check(ob) \ |
Amaury Forgeot d'Arc | cab3d98 | 2008-02-03 22:51:43 +0000 | [diff] [blame] | 77 | (Py_TYPE(ob) == &PySet_Type || \ |
| 78 | PyType_IsSubtype(Py_TYPE(ob), &PySet_Type)) |
| 79 | #define PyFrozenSet_Check(ob) \ |
Amaury Forgeot d'Arc | 58e185a | 2008-02-03 23:14:32 +0000 | [diff] [blame] | 80 | (Py_TYPE(ob) == &PyFrozenSet_Type || \ |
Amaury Forgeot d'Arc | cab3d98 | 2008-02-03 22:51:43 +0000 | [diff] [blame] | 81 | PyType_IsSubtype(Py_TYPE(ob), &PyFrozenSet_Type)) |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 82 | |
Raymond Hettinger | beb3101 | 2005-08-16 03:47:52 +0000 | [diff] [blame] | 83 | PyAPI_FUNC(PyObject *) PySet_New(PyObject *); |
| 84 | PyAPI_FUNC(PyObject *) PyFrozenSet_New(PyObject *); |
Neal Norwitz | 8c49c82 | 2006-03-04 18:41:19 +0000 | [diff] [blame] | 85 | PyAPI_FUNC(Py_ssize_t) PySet_Size(PyObject *anyset); |
Raymond Hettinger | beb3101 | 2005-08-16 03:47:52 +0000 | [diff] [blame] | 86 | #define PySet_GET_SIZE(so) (((PySetObject *)(so))->used) |
Barry Warsaw | 176014f | 2006-03-30 22:45:35 +0000 | [diff] [blame] | 87 | PyAPI_FUNC(int) PySet_Clear(PyObject *set); |
Raymond Hettinger | beb3101 | 2005-08-16 03:47:52 +0000 | [diff] [blame] | 88 | PyAPI_FUNC(int) PySet_Contains(PyObject *anyset, PyObject *key); |
Raymond Hettinger | c47e01d | 2005-08-16 10:44:15 +0000 | [diff] [blame] | 89 | PyAPI_FUNC(int) PySet_Discard(PyObject *set, PyObject *key); |
Raymond Hettinger | beb3101 | 2005-08-16 03:47:52 +0000 | [diff] [blame] | 90 | PyAPI_FUNC(int) PySet_Add(PyObject *set, PyObject *key); |
Raymond Hettinger | 0bbbfc4 | 2007-03-20 21:27:24 +0000 | [diff] [blame] | 91 | PyAPI_FUNC(int) _PySet_Next(PyObject *set, Py_ssize_t *pos, PyObject **key); |
| 92 | PyAPI_FUNC(int) _PySet_NextEntry(PyObject *set, Py_ssize_t *pos, PyObject **key, long *hash); |
Raymond Hettinger | beb3101 | 2005-08-16 03:47:52 +0000 | [diff] [blame] | 93 | PyAPI_FUNC(PyObject *) PySet_Pop(PyObject *set); |
Barry Warsaw | 176014f | 2006-03-30 22:45:35 +0000 | [diff] [blame] | 94 | PyAPI_FUNC(int) _PySet_Update(PyObject *set, PyObject *iterable); |
Raymond Hettinger | beb3101 | 2005-08-16 03:47:52 +0000 | [diff] [blame] | 95 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 96 | #ifdef __cplusplus |
| 97 | } |
| 98 | #endif |
| 99 | #endif /* !Py_SETOBJECT_H */ |