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 { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 25 | long hash; /* cached hash code for the entry key */ |
| 26 | PyObject *key; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 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 { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 36 | PyObject_HEAD |
Armin Rigo | 89a3946 | 2004-10-28 16:32:00 +0000 | [diff] [blame] | 37 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +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 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 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. |
| 44 | */ |
| 45 | Py_ssize_t mask; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 46 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 47 | /* table points to smalltable for small tables, else to |
| 48 | * additional malloc'ed memory. table is never NULL! This rule |
| 49 | * saves repeated runtime null-tests. |
| 50 | */ |
| 51 | setentry *table; |
| 52 | setentry *(*lookup)(PySetObject *so, PyObject *key, long hash); |
| 53 | setentry smalltable[PySet_MINSIZE]; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 54 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 55 | long hash; /* only used by frozenset objects */ |
| 56 | PyObject *weakreflist; /* List of weak references */ |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 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; |
Christian Heimes | a22e8bd | 2007-11-29 22:35:39 +0000 | [diff] [blame] | 61 | PyAPI_DATA(PyTypeObject) PySetIter_Type; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 62 | |
Raymond Hettinger | bc841a1 | 2005-08-07 13:02:53 +0000 | [diff] [blame] | 63 | /* Invariants for frozensets: |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 64 | * data is immutable. |
| 65 | * 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] | 66 | * Invariants for sets: |
| 67 | * hash is -1 |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 68 | */ |
| 69 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 70 | #define PyFrozenSet_CheckExact(ob) (Py_TYPE(ob) == &PyFrozenSet_Type) |
Raymond Hettinger | bc841a1 | 2005-08-07 13:02:53 +0000 | [diff] [blame] | 71 | #define PyAnySet_CheckExact(ob) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 72 | (Py_TYPE(ob) == &PySet_Type || Py_TYPE(ob) == &PyFrozenSet_Type) |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 73 | #define PyAnySet_Check(ob) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 74 | (Py_TYPE(ob) == &PySet_Type || Py_TYPE(ob) == &PyFrozenSet_Type || \ |
| 75 | PyType_IsSubtype(Py_TYPE(ob), &PySet_Type) || \ |
| 76 | PyType_IsSubtype(Py_TYPE(ob), &PyFrozenSet_Type)) |
Christian Heimes | fd66e51 | 2008-01-29 12:18:50 +0000 | [diff] [blame] | 77 | #define PySet_Check(ob) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 78 | (Py_TYPE(ob) == &PySet_Type || \ |
| 79 | PyType_IsSubtype(Py_TYPE(ob), &PySet_Type)) |
Christian Heimes | 15ebc88 | 2008-02-04 18:48:49 +0000 | [diff] [blame] | 80 | #define PyFrozenSet_Check(ob) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 81 | (Py_TYPE(ob) == &PyFrozenSet_Type || \ |
| 82 | PyType_IsSubtype(Py_TYPE(ob), &PyFrozenSet_Type)) |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 83 | |
Raymond Hettinger | beb3101 | 2005-08-16 03:47:52 +0000 | [diff] [blame] | 84 | PyAPI_FUNC(PyObject *) PySet_New(PyObject *); |
| 85 | PyAPI_FUNC(PyObject *) PyFrozenSet_New(PyObject *); |
Neal Norwitz | 8c49c82 | 2006-03-04 18:41:19 +0000 | [diff] [blame] | 86 | PyAPI_FUNC(Py_ssize_t) PySet_Size(PyObject *anyset); |
Raymond Hettinger | beb3101 | 2005-08-16 03:47:52 +0000 | [diff] [blame] | 87 | #define PySet_GET_SIZE(so) (((PySetObject *)(so))->used) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 88 | PyAPI_FUNC(int) PySet_Clear(PyObject *set); |
Raymond Hettinger | beb3101 | 2005-08-16 03:47:52 +0000 | [diff] [blame] | 89 | PyAPI_FUNC(int) PySet_Contains(PyObject *anyset, PyObject *key); |
Raymond Hettinger | c47e01d | 2005-08-16 10:44:15 +0000 | [diff] [blame] | 90 | PyAPI_FUNC(int) PySet_Discard(PyObject *set, PyObject *key); |
Raymond Hettinger | beb3101 | 2005-08-16 03:47:52 +0000 | [diff] [blame] | 91 | PyAPI_FUNC(int) PySet_Add(PyObject *set, PyObject *key); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 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); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +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 */ |