|
| 1 | +#ifndef UTILS_TN_HPP |
| 2 | +#define UTILS_TN_HPP |
| 3 | + |
| 4 | +#define PY_SSIZE_T_CLEAN |
| 5 | +#include <Python.h> |
| 6 | +#include <structmember.h> |
| 7 | +#include <iostream> |
| 8 | +#include "Node.hpp" |
| 9 | +#include "utils.hpp" |
| 10 | + |
| 11 | +typedef struct { |
| 12 | + PyObject_HEAD |
| 13 | + // long key; |
| 14 | + // long data; |
| 15 | + // PyObject* left; // can store None or a number |
| 16 | + // PyObject* right; // can store None or a number |
| 17 | + // bool is_root; |
| 18 | + // long height; |
| 19 | + // PyObject* parent; |
| 20 | + // long size; |
| 21 | +} TN; |
| 22 | + |
| 23 | +static void TN_dealloc(TN *self) { |
| 24 | + // left and right are long values, no need to dealloc for them |
| 25 | + // TN_dealloc(reinterpret_cast<TN*>(TN->left)); |
| 26 | + // TN_dealloc(reinterpret_cast<TN*>(TN->right)); |
| 27 | + // Check if other deallocs are needed using Py_XDECREF |
| 28 | + Py_TYPE(self)->tp_free(reinterpret_cast<PyObject*>(self)); |
| 29 | +} |
| 30 | + |
| 31 | +static PyObject* TN___new__(PyTypeObject* type, PyObject *args, PyObject *kwds) { |
| 32 | + TN *self; |
| 33 | + std::cout<<"h3"<<std::endl; |
| 34 | + self = reinterpret_cast<TN*>(type->tp_alloc(type, 0)); |
| 35 | + std::cout<<"h4"<<std::endl; |
| 36 | + |
| 37 | + // // Check what this is: (python code below:) |
| 38 | + // // obj = Node.__new__(cls) |
| 39 | + |
| 40 | + // // Assume that arguments are in the order below. Modify the code such that this is true. |
| 41 | + // self->key = PyLong_AsLong(PyObject_GetItem(args, PyZero)); |
| 42 | + // std::cout<<"h5"<<std::endl; |
| 43 | + // self->data = PyLong_AsLong(PyObject_GetItem(args, PyOne)); |
| 44 | + // std::cout<<"h6"<<std::endl; |
| 45 | + |
| 46 | + // Py_INCREF(Py_None); |
| 47 | + // std::cout<<"h7"<<std::endl; |
| 48 | + // self->left = Py_None; |
| 49 | + // std::cout<<"h8"<<std::endl; |
| 50 | + // Py_INCREF(Py_None); |
| 51 | + // self->right = Py_None; |
| 52 | + // Py_INCREF(Py_None); |
| 53 | + // self->parent = Py_None; |
| 54 | + // self->height = 0; |
| 55 | + // self->size = 1; |
| 56 | + // self->is_root = false; |
| 57 | + |
| 58 | + return reinterpret_cast<PyObject*>(self); |
| 59 | +} |
| 60 | + |
| 61 | +static PyObject* TN___str__(TN *self) { |
| 62 | + // PyObject* out = Py_BuildValue("(OllO)", self->left, self->key, self->data, self->right); |
| 63 | + // Py_INCREF(out); |
| 64 | + // return PyObject_Str(out); |
| 65 | +} |
| 66 | + |
| 67 | +static struct PyMemberDef TN_PyMemberDef[] = { |
| 68 | + // {"key", T_LONG, offsetof(TN, key), 0, "TN key"}, |
| 69 | + // {"data", T_LONG, offsetof(TN, data), 0, "TN data"}, |
| 70 | + // {"height", T_LONG, offsetof(TN, height), 0, "TN height"}, |
| 71 | + // {"size", T_LONG, offsetof(TN, size), 0, "TN size"}, |
| 72 | + // {"is_root", T_BOOL, offsetof(TN, is_root), 0, "TN is_root"}, |
| 73 | + // {"left", T_OBJECT, offsetof(TN, left), 0, "TN left"}, |
| 74 | + // {"right", T_OBJECT, offsetof(TN, right), 0, "TN right"}, |
| 75 | + // {"parent", T_OBJECT, offsetof(TN, parent), 0, "TN parent"}, |
| 76 | + {NULL}, |
| 77 | +}; |
| 78 | + |
| 79 | +static PyTypeObject TNType = { |
| 80 | + /* tp_name */ PyVarObject_HEAD_INIT(NULL, 0) "TN", |
| 81 | + /* tp_basicsize */ sizeof(TN), |
| 82 | + /* tp_itemsize */ 0, |
| 83 | + /* tp_dealloc */ (destructor) TN_dealloc, |
| 84 | + /* tp_print */ 0, |
| 85 | + /* tp_getattr */ 0, |
| 86 | + /* tp_setattr */ 0, |
| 87 | + /* tp_reserved */ 0, |
| 88 | + /* tp_repr */ 0, |
| 89 | + /* tp_as_number */ 0, |
| 90 | + /* tp_as_sequence */ 0, |
| 91 | + /* tp_as_mapping */ 0, |
| 92 | + /* tp_hash */ 0, |
| 93 | + /* tp_call */ 0, |
| 94 | + /* tp_str */ (reprfunc) TN___str__, |
| 95 | + /* tp_getattro */ 0, |
| 96 | + /* tp_setattro */ 0, |
| 97 | + /* tp_as_buffer */ 0, |
| 98 | + /* tp_flags */ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 99 | + /* tp_doc */ 0, |
| 100 | + /* tp_traverse */ 0, |
| 101 | + /* tp_clear */ 0, |
| 102 | + /* tp_richcompare */ 0, |
| 103 | + /* tp_weaklistoffset */ 0, |
| 104 | + /* tp_iter */ 0, |
| 105 | + /* tp_iternext */ 0, |
| 106 | + /* tp_methods */ 0, |
| 107 | + /* tp_members */ TN_PyMemberDef, |
| 108 | + /* tp_getset */ 0, |
| 109 | + /* tp_base */ &NodeType, // Class Node is the base class |
| 110 | + /* tp_dict */ 0, |
| 111 | + /* tp_descr_get */ 0, |
| 112 | + /* tp_descr_set */ 0, |
| 113 | + /* tp_dictoffset */ 0, |
| 114 | + /* tp_init */ 0, |
| 115 | + /* tp_alloc */ 0, |
| 116 | + /* tp_new */ TN___new__, |
| 117 | +}; |
| 118 | + |
| 119 | +#endif |
0 commit comments