Skip to content

Commit 3675a96

Browse files
committed
added TN for testing
1 parent 1792093 commit 3675a96

File tree

3 files changed

+129
-0
lines changed

3 files changed

+129
-0
lines changed

pydatastructs/trees/_backend/cpp/BinaryTree.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <cstdlib>
99
#include "../../../utils/_backend/cpp/utils.hpp"
1010
#include "../../../utils/_backend/cpp/TreeNode.hpp"
11+
#include "../../../utils/_backend/cpp/TN.hpp"
1112
#include "../../../linear_data_structures/_backend/cpp/arrays/ArrayForTrees.hpp"
1213

1314
typedef struct {
@@ -43,6 +44,9 @@ static PyObject* BinaryTree___new__(PyTypeObject* type, PyObject *args, PyObject
4344
key = root_data == Py_None ? Py_None : key; // This key is the argument, not self->key
4445
std::cout<<"h1"<<std::endl;
4546

47+
TN* r = reinterpret_cast<TN*>(TN___new__(&TNType, args, kwds)); // check if this is correct
48+
std::cout<<"yay! Error solved! :)"<<std::endl;
49+
4650
TreeNode* root = reinterpret_cast<TreeNode*>(TreeNode___new__(&TreeNodeType, args, kwds)); // check if this is correct
4751
std::cout<<"h2"<<std::endl;
4852
root->is_root = true;
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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

pydatastructs/utils/_backend/cpp/nodes.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,11 @@ PyMODINIT_FUNC PyInit__nodes(void) {
2626
Py_INCREF(&TreeNodeType);
2727
PyModule_AddObject(nodes, "TreeNode", reinterpret_cast<PyObject*>(&TreeNodeType));
2828

29+
if (PyType_Ready(&TNType) < 0) {
30+
return NULL;
31+
}
32+
Py_INCREF(&TNType);
33+
PyModule_AddObject(nodes, "TN", reinterpret_cast<PyObject*>(&TNType));
34+
2935
return nodes;
3036
}

0 commit comments

Comments
 (0)