| 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 | */ | 
| Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 21 | #ifndef Py_LIMITED_API | 
| Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 22 | #define PySet_MINSIZE 8 | 
 | 23 |  | 
 | 24 | typedef struct { | 
| Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 25 |     /* Cached hash code of the key. */ | 
 | 26 |     Py_hash_t hash; | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 27 |     PyObject *key; | 
| Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 28 | } setentry; | 
 | 29 |  | 
 | 30 |  | 
| Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 31 | /* | 
 | 32 | This data structure is shared by set and frozenset objects. | 
 | 33 | */ | 
 | 34 |  | 
| Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 35 | typedef struct _setobject PySetObject; | 
 | 36 | struct _setobject { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 37 |     PyObject_HEAD | 
| Armin Rigo | 89a3946 | 2004-10-28 16:32:00 +0000 | [diff] [blame] | 38 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 39 |     Py_ssize_t fill;  /* # Active + # Dummy */ | 
 | 40 |     Py_ssize_t used;  /* # Active */ | 
| Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 41 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 42 |     /* The table contains mask + 1 slots, and that's a power of 2. | 
 | 43 |      * We store the mask instead of the size because the mask is more | 
 | 44 |      * frequently needed. | 
 | 45 |      */ | 
 | 46 |     Py_ssize_t mask; | 
| Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 47 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 48 |     /* table points to smalltable for small tables, else to | 
 | 49 |      * additional malloc'ed memory.  table is never NULL!  This rule | 
 | 50 |      * saves repeated runtime null-tests. | 
 | 51 |      */ | 
 | 52 |     setentry *table; | 
| Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 53 |     setentry *(*lookup)(PySetObject *so, PyObject *key, Py_hash_t hash); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 54 |     setentry smalltable[PySet_MINSIZE]; | 
| Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 55 |  | 
| Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 56 |     Py_hash_t hash;                  /* only used by frozenset objects */ | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 57 |     PyObject *weakreflist;      /* List of weak references */ | 
| Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 58 | }; | 
| Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 59 | #endif /* Py_LIMITED_API */ | 
| Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 60 |  | 
 | 61 | PyAPI_DATA(PyTypeObject) PySet_Type; | 
 | 62 | PyAPI_DATA(PyTypeObject) PyFrozenSet_Type; | 
| Christian Heimes | a22e8bd | 2007-11-29 22:35:39 +0000 | [diff] [blame] | 63 | PyAPI_DATA(PyTypeObject) PySetIter_Type; | 
| Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 64 |  | 
| Raymond Hettinger | bc841a1 | 2005-08-07 13:02:53 +0000 | [diff] [blame] | 65 | /* Invariants for frozensets: | 
| Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 66 |  *     data is immutable. | 
 | 67 |  *     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] | 68 |  * Invariants for sets: | 
 | 69 |  *     hash is -1 | 
| Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 70 |  */ | 
 | 71 |  | 
| Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 72 | #define PyFrozenSet_CheckExact(ob) (Py_TYPE(ob) == &PyFrozenSet_Type) | 
| Raymond Hettinger | bc841a1 | 2005-08-07 13:02:53 +0000 | [diff] [blame] | 73 | #define PyAnySet_CheckExact(ob) \ | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 74 |     (Py_TYPE(ob) == &PySet_Type || Py_TYPE(ob) == &PyFrozenSet_Type) | 
| Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 75 | #define PyAnySet_Check(ob) \ | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 76 |     (Py_TYPE(ob) == &PySet_Type || Py_TYPE(ob) == &PyFrozenSet_Type || \ | 
 | 77 |       PyType_IsSubtype(Py_TYPE(ob), &PySet_Type) || \ | 
 | 78 |       PyType_IsSubtype(Py_TYPE(ob), &PyFrozenSet_Type)) | 
| Christian Heimes | fd66e51 | 2008-01-29 12:18:50 +0000 | [diff] [blame] | 79 | #define PySet_Check(ob) \ | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 80 |     (Py_TYPE(ob) == &PySet_Type || \ | 
 | 81 |     PyType_IsSubtype(Py_TYPE(ob), &PySet_Type)) | 
| Christian Heimes | 15ebc88 | 2008-02-04 18:48:49 +0000 | [diff] [blame] | 82 | #define   PyFrozenSet_Check(ob) \ | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 83 |     (Py_TYPE(ob) == &PyFrozenSet_Type || \ | 
 | 84 |       PyType_IsSubtype(Py_TYPE(ob), &PyFrozenSet_Type)) | 
| Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 85 |  | 
| Raymond Hettinger | beb3101 | 2005-08-16 03:47:52 +0000 | [diff] [blame] | 86 | PyAPI_FUNC(PyObject *) PySet_New(PyObject *); | 
 | 87 | PyAPI_FUNC(PyObject *) PyFrozenSet_New(PyObject *); | 
| Neal Norwitz | 8c49c82 | 2006-03-04 18:41:19 +0000 | [diff] [blame] | 88 | PyAPI_FUNC(Py_ssize_t) PySet_Size(PyObject *anyset); | 
| Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 89 | #ifndef Py_LIMITED_API | 
| Raymond Hettinger | beb3101 | 2005-08-16 03:47:52 +0000 | [diff] [blame] | 90 | #define PySet_GET_SIZE(so) (((PySetObject *)(so))->used) | 
| Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 91 | #endif | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 92 | PyAPI_FUNC(int) PySet_Clear(PyObject *set); | 
| Raymond Hettinger | beb3101 | 2005-08-16 03:47:52 +0000 | [diff] [blame] | 93 | PyAPI_FUNC(int) PySet_Contains(PyObject *anyset, PyObject *key); | 
| Raymond Hettinger | c47e01d | 2005-08-16 10:44:15 +0000 | [diff] [blame] | 94 | PyAPI_FUNC(int) PySet_Discard(PyObject *set, PyObject *key); | 
| Raymond Hettinger | beb3101 | 2005-08-16 03:47:52 +0000 | [diff] [blame] | 95 | PyAPI_FUNC(int) PySet_Add(PyObject *set, PyObject *key); | 
| Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 96 | #ifndef Py_LIMITED_API | 
| Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 97 | PyAPI_FUNC(int) _PySet_NextEntry(PyObject *set, Py_ssize_t *pos, PyObject **key, Py_hash_t *hash); | 
| Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 98 | #endif | 
| Raymond Hettinger | beb3101 | 2005-08-16 03:47:52 +0000 | [diff] [blame] | 99 | PyAPI_FUNC(PyObject *) PySet_Pop(PyObject *set); | 
| Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 100 | #ifndef Py_LIMITED_API | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 101 | PyAPI_FUNC(int) _PySet_Update(PyObject *set, PyObject *iterable); | 
| Antoine Pitrou | 093ce9c | 2011-12-16 11:24:27 +0100 | [diff] [blame] | 102 |  | 
 | 103 | PyAPI_FUNC(int) PySet_ClearFreeList(void); | 
| Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 104 | #endif | 
| Raymond Hettinger | beb3101 | 2005-08-16 03:47:52 +0000 | [diff] [blame] | 105 |  | 
| Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 106 | #ifdef __cplusplus | 
 | 107 | } | 
 | 108 | #endif | 
 | 109 | #endif /* !Py_SETOBJECT_H */ |