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 | 404a45d | 2014-12-26 17:28:16 -0800 | [diff] [blame] | 9 | #ifndef Py_LIMITED_API |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 10 | |
Raymond Hettinger | 404a45d | 2014-12-26 17:28:16 -0800 | [diff] [blame] | 11 | /* There are three kinds of entries in the table: |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 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 | */ |
Raymond Hettinger | 404a45d | 2014-12-26 17:28:16 -0800 | [diff] [blame] | 21 | |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 22 | #define PySet_MINSIZE 8 |
| 23 | |
| 24 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 25 | PyObject *key; |
Raymond Hettinger | 3f063a5 | 2014-12-28 17:15:12 -0800 | [diff] [blame] | 26 | Py_hash_t hash; /* Cached hash code of the key */ |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 27 | } setentry; |
| 28 | |
Raymond Hettinger | 404a45d | 2014-12-26 17:28:16 -0800 | [diff] [blame] | 29 | /* The SetObject data structure is shared by set and frozenset objects. |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 30 | |
Raymond Hettinger | 404a45d | 2014-12-26 17:28:16 -0800 | [diff] [blame] | 31 | Invariant for sets: |
| 32 | - hash is -1 |
| 33 | |
| 34 | Invariants for frozensets: |
| 35 | - data is immutable. |
| 36 | - hash is the hash of the frozenset or -1 if not computed yet. |
| 37 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 38 | */ |
| 39 | |
Raymond Hettinger | 404a45d | 2014-12-26 17:28:16 -0800 | [diff] [blame] | 40 | typedef struct _setobject { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 41 | PyObject_HEAD |
Armin Rigo | 89a3946 | 2004-10-28 16:32:00 +0000 | [diff] [blame] | 42 | |
Raymond Hettinger | 404a45d | 2014-12-26 17:28:16 -0800 | [diff] [blame] | 43 | Py_ssize_t fill; /* Number active and dummy entries*/ |
| 44 | Py_ssize_t used; /* Number active entries */ |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 45 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 46 | /* The table contains mask + 1 slots, and that's a power of 2. |
| 47 | * We store the mask instead of the size because the mask is more |
| 48 | * frequently needed. |
| 49 | */ |
| 50 | Py_ssize_t mask; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 51 | |
Raymond Hettinger | 404a45d | 2014-12-26 17:28:16 -0800 | [diff] [blame] | 52 | /* The table points to a fixed-size smalltable for small tables |
| 53 | * or to additional malloc'ed memory for bigger tables. |
| 54 | * The table pointer is never NULL which saves us from repeated |
| 55 | * runtime null-tests. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 56 | */ |
| 57 | setentry *table; |
Raymond Hettinger | 404a45d | 2014-12-26 17:28:16 -0800 | [diff] [blame] | 58 | setentry *(*lookup)(struct _setobject *so, PyObject *key, Py_hash_t hash); |
| 59 | Py_hash_t hash; /* Only used by frozenset objects */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 60 | setentry smalltable[PySet_MINSIZE]; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 61 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 62 | PyObject *weakreflist; /* List of weak references */ |
Raymond Hettinger | 404a45d | 2014-12-26 17:28:16 -0800 | [diff] [blame] | 63 | } PySetObject; |
| 64 | |
| 65 | #define PySet_GET_SIZE(so) (((PySetObject *)(so))->used) |
| 66 | |
| 67 | PyAPI_DATA(PyObject *) _PySet_Dummy; |
| 68 | |
| 69 | PyAPI_FUNC(int) _PySet_NextEntry(PyObject *set, Py_ssize_t *pos, PyObject **key, Py_hash_t *hash); |
| 70 | PyAPI_FUNC(int) _PySet_Update(PyObject *set, PyObject *iterable); |
| 71 | PyAPI_FUNC(int) PySet_ClearFreeList(void); |
| 72 | |
| 73 | #endif /* Section excluded by Py_LIMITED_API */ |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 74 | |
| 75 | PyAPI_DATA(PyTypeObject) PySet_Type; |
| 76 | PyAPI_DATA(PyTypeObject) PyFrozenSet_Type; |
Christian Heimes | a22e8bd | 2007-11-29 22:35:39 +0000 | [diff] [blame] | 77 | PyAPI_DATA(PyTypeObject) PySetIter_Type; |
Antoine Pitrou | 9d95254 | 2013-08-24 21:07:07 +0200 | [diff] [blame] | 78 | |
Raymond Hettinger | 404a45d | 2014-12-26 17:28:16 -0800 | [diff] [blame] | 79 | PyAPI_FUNC(PyObject *) PySet_New(PyObject *); |
| 80 | PyAPI_FUNC(PyObject *) PyFrozenSet_New(PyObject *); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 81 | |
Raymond Hettinger | 404a45d | 2014-12-26 17:28:16 -0800 | [diff] [blame] | 82 | PyAPI_FUNC(int) PySet_Add(PyObject *set, PyObject *key); |
| 83 | PyAPI_FUNC(int) PySet_Clear(PyObject *set); |
| 84 | PyAPI_FUNC(int) PySet_Contains(PyObject *anyset, PyObject *key); |
| 85 | PyAPI_FUNC(int) PySet_Discard(PyObject *set, PyObject *key); |
| 86 | PyAPI_FUNC(PyObject *) PySet_Pop(PyObject *set); |
| 87 | PyAPI_FUNC(Py_ssize_t) PySet_Size(PyObject *anyset); |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 88 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 89 | #define PyFrozenSet_CheckExact(ob) (Py_TYPE(ob) == &PyFrozenSet_Type) |
Raymond Hettinger | bc841a1 | 2005-08-07 13:02:53 +0000 | [diff] [blame] | 90 | #define PyAnySet_CheckExact(ob) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 91 | (Py_TYPE(ob) == &PySet_Type || Py_TYPE(ob) == &PyFrozenSet_Type) |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 92 | #define PyAnySet_Check(ob) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 93 | (Py_TYPE(ob) == &PySet_Type || Py_TYPE(ob) == &PyFrozenSet_Type || \ |
| 94 | PyType_IsSubtype(Py_TYPE(ob), &PySet_Type) || \ |
| 95 | PyType_IsSubtype(Py_TYPE(ob), &PyFrozenSet_Type)) |
Christian Heimes | fd66e51 | 2008-01-29 12:18:50 +0000 | [diff] [blame] | 96 | #define PySet_Check(ob) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 97 | (Py_TYPE(ob) == &PySet_Type || \ |
| 98 | PyType_IsSubtype(Py_TYPE(ob), &PySet_Type)) |
Christian Heimes | 15ebc88 | 2008-02-04 18:48:49 +0000 | [diff] [blame] | 99 | #define PyFrozenSet_Check(ob) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 100 | (Py_TYPE(ob) == &PyFrozenSet_Type || \ |
| 101 | PyType_IsSubtype(Py_TYPE(ob), &PyFrozenSet_Type)) |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 102 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 103 | #ifdef __cplusplus |
| 104 | } |
| 105 | #endif |
| 106 | #endif /* !Py_SETOBJECT_H */ |