1 /** 2 Mirror _listobject.h 3 4 Another generally useful object type is an list of object pointers. 5 This is a mutable type: the list items can be changed, and items can be 6 added or removed. Out-of-range indices or non-list objects are ignored. 7 */ 8 module deimos.python.listobject; 9 10 import deimos.python.pyport; 11 import deimos.python.object; 12 13 extern(C): 14 // Python-header-file: Include/listobject.h: 15 16 /** ob_item contains space for 'allocated' elements. The number 17 * currently in use is ob_size. 18 * Invariants: 19 * 0 <= ob_size <= allocated 20 * len(list) == ob_size 21 * ob_item == NULL implies ob_size == allocated == 0 22 * list.sort() temporarily sets allocated to -1 to detect mutations. 23 * 24 * Items must normally not be NULL, except during construction when 25 * the list is not yet visible outside the function that builds it. 26 * 27 * subclass of PyObject. 28 */ 29 struct PyListObject { 30 mixin PyObject_VAR_HEAD; 31 32 /** Vector of pointers to list elements. list[0] is ob_item[0], etc. */ 33 PyObject** ob_item; 34 /// _ 35 Py_ssize_t allocated; 36 } 37 38 /// _ 39 mixin(PyAPI_DATA!"PyTypeObject PyList_Type"); 40 version(Python_3_0_Or_Later) { 41 /// Availability: 3.* 42 mixin(PyAPI_DATA!"PyTypeObject PyListIter_Type"); 43 /// Availability: 3.* 44 mixin(PyAPI_DATA!"PyTypeObject PyListRevIter_Type"); 45 /// Availability: 3.* 46 mixin(PyAPI_DATA!"PyTypeObject PySortWrapper_Type"); 47 } 48 49 // D translation of C macro: 50 /// _ 51 int PyList_Check()(PyObject* op) { 52 return PyObject_TypeCheck(op, &PyList_Type); 53 } 54 // D translation of C macro: 55 /// _ 56 int PyList_CheckExact()(PyObject* op) { 57 return Py_TYPE(op) == &PyList_Type; 58 } 59 60 /// _ 61 PyObject* PyList_New(Py_ssize_t size); 62 /// _ 63 Py_ssize_t PyList_Size(PyObject*); 64 65 /** WARNING: PyList_SetItem does not increment the new item's reference 66 count, but does decrement the reference count of the item it replaces, 67 if not nil. It does *decrement* the reference count if it is *not* 68 inserted in the list. Similarly, PyList_GetItem does not increment the 69 returned item's reference count. 70 */ 71 PyObject_BorrowedRef* PyList_GetItem(PyObject*, Py_ssize_t); 72 /// ditto 73 int PyList_SetItem(PyObject*, Py_ssize_t, PyObject*); 74 /// _ 75 int PyList_Insert(PyObject*, Py_ssize_t, PyObject*); 76 /// _ 77 int PyList_Append(PyObject*, PyObject*); 78 /// _ 79 PyObject* PyList_GetSlice(PyObject*, Py_ssize_t, Py_ssize_t); 80 /// _ 81 int PyList_SetSlice(PyObject*, Py_ssize_t, Py_ssize_t, PyObject*); 82 /// _ 83 int PyList_Sort(PyObject*); 84 /// _ 85 int PyList_Reverse(PyObject*); 86 /// _ 87 PyObject* PyList_AsTuple(PyObject*); 88 89 // D translations of C macros: 90 /// _ 91 PyObject_BorrowedRef* PyList_GET_ITEM()(PyObject* op, Py_ssize_t i) { 92 return (cast(PyListObject*) op).ob_item[i]; 93 } 94 /// _ 95 void PyList_SET_ITEM()(PyObject* op, Py_ssize_t i, PyObject* v) { 96 (cast(PyListObject*)op).ob_item[i] = v; 97 } 98 /// _ 99 Py_ssize_t PyList_GET_SIZE()(PyObject* op) { 100 return (cast(PyListObject*) op).ob_size; 101 } 102 103