1 /** 2 Mirror _setobject.h 3 4 Set object interface 5 */ 6 module deimos.python.setobject; 7 8 import core.stdc.stdio; 9 import deimos.python.pyport; 10 import deimos.python.object; 11 12 extern(C): 13 // Python-header-file: Include/setobject.h: 14 15 version(Python_2_5_Or_Later){ 16 /// Availability: >= 2.5 17 enum PySet_MINSIZE = 8; 18 19 /// Availability: >= 2.5 20 struct setentry { 21 /** cached hash code for the entry key */ 22 Py_hash_t hash; 23 /// _ 24 PyObject* key; 25 } 26 } 27 28 /** 29 This data structure is shared by set and frozenset objects. 30 31 Invariants for frozensets: 32 data is immutable. 33 hash is the hash of the frozenset or -1 if not computed yet. 34 Invariants for sets: 35 hash is -1 36 37 subclass of PyObject. 38 */ 39 struct PySetObject { 40 mixin PyObject_HEAD; 41 42 version(Python_2_5_Or_Later){ 43 /// Availability: >= 2.5 44 Py_ssize_t fill; 45 /// Availability: >= 2.5 46 Py_ssize_t used; 47 48 /** The table contains mask + 1 slots, and that's a power of 2. 49 * We store the mask instead of the size because the mask is more 50 * frequently needed. 51 */ 52 Py_ssize_t mask; 53 54 /** table points to smalltable for small tables, else to 55 * additional malloc'ed memory. table is never NULL! This rule 56 * saves repeated runtime null-tests. 57 */ 58 setentry* table; 59 /// _ 60 version(Python_3_2_Or_Later) { 61 setentry* function(PySetObject* so, PyObject* key, Py_hash_t hash) lookup; 62 }else{ 63 setentry* function(PySetObject* so, PyObject* key, C_long hash) lookup; 64 } 65 /// _ 66 setentry[PySet_MINSIZE] smalltable; 67 }else{ 68 /// Availability: 2.4 69 PyObject* data; 70 } 71 72 /** only used by frozenset objects */ 73 Py_hash_t hash; 74 /** List of weak references */ 75 PyObject* weakreflist; 76 } 77 78 /// _ 79 mixin(PyAPI_DATA!"PyTypeObject PySet_Type"); 80 /// _ 81 mixin(PyAPI_DATA!"PyTypeObject PyFrozenSet_Type"); 82 /// _ 83 mixin(PyAPI_DATA!"PyTypeObject PySetIter_Type"); 84 85 // D translations of C macros: 86 /// _ 87 int PyFrozenSet_CheckExact()(PyObject* ob) { 88 return Py_TYPE(ob) == &PyFrozenSet_Type; 89 } 90 /// _ 91 int PyAnySet_CheckExact()(PyObject* ob) { 92 return Py_TYPE(ob) == &PySet_Type || Py_TYPE(ob) == &PyFrozenSet_Type; 93 } 94 /// _ 95 int PyAnySet_Check()(PyObject* ob) { 96 return ( 97 Py_TYPE(ob) == &PySet_Type 98 || Py_TYPE(ob) == &PyFrozenSet_Type 99 || PyType_IsSubtype(Py_TYPE(ob), &PySet_Type) 100 || PyType_IsSubtype(Py_TYPE(ob), &PyFrozenSet_Type) 101 ); 102 } 103 version(Python_2_6_Or_Later){ 104 /// Availability: >= 2.6 105 bool PySet_Check()(PyObject* ob) { 106 return (Py_TYPE(ob) == &PySet_Type || 107 PyType_IsSubtype(Py_TYPE(ob), &PySet_Type)); 108 } 109 /// Availability: >= 2.6 110 bool PyFrozenSet_Check()(PyObject* ob) { 111 return (Py_TYPE(ob) == &PyFrozenSet_Type || 112 PyType_IsSubtype(Py_TYPE(ob), &PyFrozenSet_Type)); 113 } 114 } 115 116 version(Python_2_5_Or_Later){ 117 /// Availability: >= 2.5 118 PyObject* PySet_New(PyObject*); 119 /// Availability: >= 2.5 120 PyObject* PyFrozenSet_New(PyObject*); 121 /// Availability: >= 2.5 122 Py_ssize_t PySet_Size(PyObject* anyset); 123 /// Availability: >= 2.5 124 Py_ssize_t PySet_GET_SIZE()(PyObject* so) { 125 return (cast(PySetObject*)so).used; 126 } 127 /// Availability: >= 2.5 128 int PySet_Clear(PyObject* set); 129 /// Availability: >= 2.5 130 int PySet_Contains(PyObject* anyset, PyObject* key); 131 /// Availability: >= 2.5 132 int PySet_Discard(PyObject* set, PyObject* key); 133 /// Availability: >= 2.5 134 int PySet_Add(PyObject* set, PyObject* key); 135 /// Availability: >= 2.5 136 int _PySet_Next(PyObject* set, Py_ssize_t *pos, PyObject** entry); 137 /// Availability: >= 2.5 138 int _PySet_NextEntry(PyObject* set, Py_ssize_t* pos, PyObject** key, Py_hash_t* hash); 139 /// Availability: >= 2.5 140 PyObject* PySet_Pop(PyObject* set); 141 /// Availability: >= 2.5 142 int _PySet_Update(PyObject* set, PyObject* iterable); 143 } 144 145 version(Python_3_2_Or_Later) { 146 /// Availability: >= 3.2 147 void _PySet_DebugMallocStats(FILE* out_); 148 }