|
| 1 | +#ifndef TREES_REDBLACKTREE_HPP |
| 2 | +#define TREES_REDBLACKTREE_HPP |
| 3 | + |
| 4 | +#define PY_SSIZE_T_CLEAN |
| 5 | +#include <Python.h> |
| 6 | +#include <structmember.h> |
| 7 | +#include <cstdlib> |
| 8 | +#include "../../../utils/_backend/cpp/utils.hpp" |
| 9 | +#include "../../../utils/_backend/cpp/TreeNode.hpp" |
| 10 | +#include "../../../linear_data_structures/_backend/cpp/arrays/ArrayForTrees.hpp" |
| 11 | +#include "../../../linear_data_structures/_backend/cpp/arrays/DynamicOneDimensionalArray.hpp" |
| 12 | +#include "BinarySearchTree.hpp" |
| 13 | +#include "SelfBalancingBinaryTree.hpp" |
| 14 | + |
| 15 | +typedef struct { |
| 16 | + PyObject_HEAD |
| 17 | + SelfBalancingBinaryTree* sbbt; |
| 18 | + ArrayForTrees* tree; |
| 19 | +} RedBlackTree; |
| 20 | + |
| 21 | +static void RedBlackTree_dealloc(RedBlackTree *self) { |
| 22 | + Py_TYPE(self)->tp_free(reinterpret_cast<PyObject*>(self)); |
| 23 | +} |
| 24 | + |
| 25 | +static PyObject* RedBlackTree___new__(PyTypeObject* type, PyObject *args, PyObject *kwds) { |
| 26 | + RedBlackTree *self; |
| 27 | + self = reinterpret_cast<RedBlackTree*>(type->tp_alloc(type, 0)); |
| 28 | + |
| 29 | + if (PyType_Ready(&SelfBalancingBinaryTreeType) < 0) { // This has to be present to finalize a type object. This should be called on all type objects to finish their initialization. |
| 30 | + return NULL; |
| 31 | + } |
| 32 | + PyObject* p = SelfBalancingBinaryTree___new__(&SelfBalancingBinaryTreeType, args, kwds); |
| 33 | + self->sbbt = reinterpret_cast<SelfBalancingBinaryTree*>(p); |
| 34 | + self->tree = reinterpret_cast<SelfBalancingBinaryTree*>(p)->bst->binary_tree->tree; |
| 35 | + |
| 36 | + return reinterpret_cast<PyObject*>(self); |
| 37 | +} |
| 38 | + |
| 39 | +static PyObject* RedBlackTree___str__(RedBlackTree *self) { |
| 40 | + return BinarySearchTree___str__(self->sbbt->bst); |
| 41 | +} |
| 42 | + |
| 43 | + |
| 44 | + |
| 45 | +static struct PyMethodDef RedBlackTree_PyMethodDef[] = { |
| 46 | + {NULL} |
| 47 | +}; |
| 48 | + |
| 49 | +static PyMemberDef RedBlackTree_PyMemberDef[] = { |
| 50 | + {"tree", T_OBJECT_EX, offsetof(RedBlackTree, tree), 0, "tree"}, |
| 51 | + {NULL} /* Sentinel */ |
| 52 | +}; |
| 53 | + |
| 54 | + |
| 55 | +static PyTypeObject RedBlackTreeType = { |
| 56 | + /* tp_name */ PyVarObject_HEAD_INIT(NULL, 0) "RedBlackTree", |
| 57 | + /* tp_basicsize */ sizeof(RedBlackTree), |
| 58 | + /* tp_itemsize */ 0, |
| 59 | + /* tp_dealloc */ (destructor) RedBlackTree_dealloc, |
| 60 | + /* tp_print */ 0, |
| 61 | + /* tp_getattr */ 0, |
| 62 | + /* tp_setattr */ 0, |
| 63 | + /* tp_reserved */ 0, |
| 64 | + /* tp_repr */ 0, |
| 65 | + /* tp_as_number */ 0, |
| 66 | + /* tp_as_sequence */ 0, |
| 67 | + /* tp_as_mapping */ 0, |
| 68 | + /* tp_hash */ 0, |
| 69 | + /* tp_call */ 0, |
| 70 | + /* tp_str */ (reprfunc) RedBlackTree___str__, |
| 71 | + /* tp_getattro */ 0, |
| 72 | + /* tp_setattro */ 0, |
| 73 | + /* tp_as_buffer */ 0, |
| 74 | + /* tp_flags */ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 75 | + /* tp_doc */ 0, |
| 76 | + /* tp_traverse */ 0, |
| 77 | + /* tp_clear */ 0, |
| 78 | + /* tp_richcompare */ 0, |
| 79 | + /* tp_weaklistoffset */ 0, |
| 80 | + /* tp_iter */ 0, |
| 81 | + /* tp_iternext */ 0, |
| 82 | + /* tp_methods */ RedBlackTree_PyMethodDef, |
| 83 | + /* tp_members */ RedBlackTree_PyMemberDef, |
| 84 | + /* tp_getset */ 0, |
| 85 | + /* tp_base */ &SelfBalancingBinaryTreeType, |
| 86 | + /* tp_dict */ 0, |
| 87 | + /* tp_descr_get */ 0, |
| 88 | + /* tp_descr_set */ 0, |
| 89 | + /* tp_dictoffset */ 0, |
| 90 | + /* tp_init */ 0, |
| 91 | + /* tp_alloc */ 0, |
| 92 | + /* tp_new */ RedBlackTree___new__, |
| 93 | +}; |
| 94 | + |
| 95 | +#endif |
0 commit comments