|
| 1 | +#ifndef MISCELLANEOUS_DATA_STRUCTURES_ARRAYSTACK_HPP |
| 2 | +#define MISCELLANEOUS_DATA_STRUCTURES_ARRAYSTACK_HPP |
| 3 | + |
| 4 | +#define PY_SSIZE_T_CLEAN |
| 5 | +#include <Python.h> |
| 6 | +#include <cstdlib> |
| 7 | +#include <iostream> |
| 8 | +#include <structmember.h> |
| 9 | +#include "../../../../linear_data_structures/_backend/cpp/arrays/DynamicOneDimensionalArray.hpp" |
| 10 | + |
| 11 | +typedef struct { |
| 12 | + PyObject_HEAD |
| 13 | + DynamicOneDimensionalArray* _items; |
| 14 | +} ArrayStack; |
| 15 | + |
| 16 | +static void ArrayStack_dealloc(ArrayStack *self) { |
| 17 | + DynamicOneDimensionalArray_dealloc(self->_items); |
| 18 | + Py_TYPE(self)->tp_free(reinterpret_cast<PyObject*>(self)); |
| 19 | +} |
| 20 | + |
| 21 | +static PyObject* ArrayStack__new__(PyTypeObject *type, PyObject *args, PyObject *kwds) { |
| 22 | + ArrayStack *self = reinterpret_cast<ArrayStack*>(type->tp_alloc(type, 0)); |
| 23 | + |
| 24 | + static char *kwlist[] = {"items", "dtype", NULL}; |
| 25 | + PyObject *initial_values = Py_None, *dtype = Py_None; |
| 26 | + if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OO", kwlist, &initial_values, &dtype)) { |
| 27 | + PyErr_SetString(PyExc_ValueError, "Error creating ArrayStack"); |
| 28 | + return NULL; |
| 29 | + } |
| 30 | + |
| 31 | + if (initial_values != Py_None && PyType_Check(initial_values)) { |
| 32 | + PyErr_SetString(PyExc_TypeError, "`items` must be an instance of list or tuple, received a type instead\n" |
| 33 | + "Did you mean to instantiate an ArrayStack with only the data type? " |
| 34 | + "if so, send the type parameter as a named argument " |
| 35 | + "for example: dtype=int"); |
| 36 | + return NULL; |
| 37 | + } |
| 38 | + |
| 39 | + PyObject* items = NULL; |
| 40 | + PyObject* doda_kwds = Py_BuildValue("{}"); |
| 41 | + if (initial_values == Py_None) { |
| 42 | + // If the only argument is the dtype, redefine the args as a tuple (dtype, 0) |
| 43 | + // where 0 is the initial array size |
| 44 | + PyObject* extended_args = PyTuple_Pack(2, dtype, PyLong_FromLong(0)); |
| 45 | + |
| 46 | + items = DynamicOneDimensionalArray___new__(&DynamicOneDimensionalArrayType, extended_args, doda_kwds); |
| 47 | + } else { |
| 48 | + // If the user provides dtype and initial values list, let the array initializer handle the checks. |
| 49 | + PyObject* doda_args = PyTuple_Pack(2, dtype, initial_values); |
| 50 | + items = DynamicOneDimensionalArray___new__(&DynamicOneDimensionalArrayType, doda_args, doda_kwds); |
| 51 | + } |
| 52 | + |
| 53 | + if (!items) { |
| 54 | + return NULL; |
| 55 | + } |
| 56 | + |
| 57 | + DynamicOneDimensionalArray* tmp = self->_items; |
| 58 | + self->_items = reinterpret_cast<DynamicOneDimensionalArray*>(items); |
| 59 | + |
| 60 | + return reinterpret_cast<PyObject*>(self); |
| 61 | +} |
| 62 | + |
| 63 | +static PyObject* ArrayStack_is_empty(ArrayStack *self) { |
| 64 | + bool is_empty = self->_items->_last_pos_filled == -1; |
| 65 | + |
| 66 | + if (is_empty) { |
| 67 | + Py_RETURN_TRUE; |
| 68 | + } |
| 69 | + |
| 70 | + Py_RETURN_FALSE; |
| 71 | +} |
| 72 | + |
| 73 | +static PyObject* ArrayStack_push(ArrayStack *self, PyObject* args) { |
| 74 | + size_t len_args = PyObject_Length(args); |
| 75 | + if (len_args != 1) { |
| 76 | + PyErr_SetString(PyExc_ValueError, "Expected one argument"); |
| 77 | + return NULL; |
| 78 | + } |
| 79 | + |
| 80 | + if (PyObject_IsTrue(ArrayStack_is_empty(self))) { |
| 81 | + self->_items->_one_dimensional_array->_dtype = reinterpret_cast<PyObject*>( |
| 82 | + Py_TYPE(PyObject_GetItem(args, PyZero)) |
| 83 | + ); |
| 84 | + } |
| 85 | + |
| 86 | + DynamicOneDimensionalArray_append(self->_items, args); |
| 87 | + |
| 88 | + Py_RETURN_NONE; |
| 89 | +} |
| 90 | + |
| 91 | +static PyObject* ArrayStack_pop(ArrayStack *self) { |
| 92 | + if (PyObject_IsTrue(ArrayStack_is_empty(self))) { |
| 93 | + PyErr_SetString(PyExc_IndexError, "Stack is empty"); |
| 94 | + return NULL; |
| 95 | + } |
| 96 | + |
| 97 | + PyObject *top_element = DynamicOneDimensionalArray___getitem__( |
| 98 | + self->_items, PyLong_FromLong(self->_items->_last_pos_filled) |
| 99 | + ); |
| 100 | + |
| 101 | + PyObject* last_pos_arg = PyTuple_Pack(1, PyLong_FromLong(self->_items->_last_pos_filled)); |
| 102 | + DynamicOneDimensionalArray_delete(self->_items, last_pos_arg); |
| 103 | + return top_element; |
| 104 | +} |
| 105 | + |
| 106 | +static PyObject* ArrayStack_peek(ArrayStack *self, void *closure) { |
| 107 | + return DynamicOneDimensionalArray___getitem__( |
| 108 | + self->_items, PyLong_FromLong(self->_items->_last_pos_filled) |
| 109 | + ); |
| 110 | +} |
| 111 | + |
| 112 | +static Py_ssize_t ArrayStack__len__(ArrayStack *self) { |
| 113 | + return self->_items->_num; |
| 114 | +} |
| 115 | + |
| 116 | +static PyObject* ArrayStack__str__(ArrayStack* self){ |
| 117 | + return DynamicOneDimensionalArray___str__(self->_items); |
| 118 | +} |
| 119 | + |
| 120 | +static struct PyMethodDef ArrayStack_PyMethodDef[] = { |
| 121 | + {"push", (PyCFunction) ArrayStack_push, METH_VARARGS, NULL}, |
| 122 | + {"pop", (PyCFunction) ArrayStack_pop, METH_VARARGS, NULL}, |
| 123 | + {NULL} |
| 124 | +}; |
| 125 | + |
| 126 | +static PyMappingMethods ArrayStack_PyMappingMethods = { |
| 127 | + (lenfunc) ArrayStack__len__, |
| 128 | +}; |
| 129 | + |
| 130 | +static PyGetSetDef ArrayStack_GetterSetters[] = { |
| 131 | + {"peek", (getter) ArrayStack_peek, NULL, "peek top value", NULL}, |
| 132 | + {"is_empty", (getter) ArrayStack_is_empty, NULL, "check if the stack is empty", NULL}, |
| 133 | + {NULL} /* Sentinel */ |
| 134 | +}; |
| 135 | + |
| 136 | +static PyTypeObject ArrayStackType = { |
| 137 | + /* tp_name */ PyVarObject_HEAD_INIT(NULL, 0) "ArrayStack", |
| 138 | + /* tp_basicsize */ sizeof(ArrayStack), |
| 139 | + /* tp_itemsize */ 0, |
| 140 | + /* tp_dealloc */ (destructor) ArrayStack_dealloc, |
| 141 | + /* tp_print */ 0, |
| 142 | + /* tp_getattr */ 0, |
| 143 | + /* tp_setattr */ 0, |
| 144 | + /* tp_reserved */ 0, |
| 145 | + /* tp_repr */ 0, |
| 146 | + /* tp_as_number */ 0, |
| 147 | + /* tp_as_sequence */ 0, |
| 148 | + /* tp_as_mapping */ &ArrayStack_PyMappingMethods, |
| 149 | + /* tp_hash */ 0, |
| 150 | + /* tp_call */ 0, |
| 151 | + /* tp_str */ (reprfunc) ArrayStack__str__, |
| 152 | + /* tp_getattro */ 0, |
| 153 | + /* tp_setattro */ 0, |
| 154 | + /* tp_as_buffer */ 0, |
| 155 | + /* tp_flags */ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 156 | + /* tp_doc */ 0, |
| 157 | + /* tp_traverse */ 0, |
| 158 | + /* tp_clear */ 0, |
| 159 | + /* tp_richcompare */ 0, |
| 160 | + /* tp_weaklistoffset */ 0, |
| 161 | + /* tp_iter */ 0, |
| 162 | + /* tp_iternext */ 0, |
| 163 | + /* tp_methods */ ArrayStack_PyMethodDef, |
| 164 | + /* tp_members */ 0, |
| 165 | + /* tp_getset */ ArrayStack_GetterSetters, |
| 166 | + /* tp_base */ 0, |
| 167 | + /* tp_dict */ 0, |
| 168 | + /* tp_descr_get */ 0, |
| 169 | + /* tp_descr_set */ 0, |
| 170 | + /* tp_dictoffset */ 0, |
| 171 | + /* tp_init */ 0, |
| 172 | + /* tp_alloc */ 0, |
| 173 | + /* tp_new */ ArrayStack__new__, |
| 174 | +}; |
| 175 | + |
| 176 | + |
| 177 | +#endif |
0 commit comments