Skip to content

Commit ece6bb8

Browse files
committed
Testing ArrayForTrees
1 parent bd74ae5 commit ece6bb8

File tree

2 files changed

+94
-61
lines changed

2 files changed

+94
-61
lines changed

pydatastructs/linear_data_structures/_backend/cpp/arrays/ArrayForTrees.hpp

Lines changed: 85 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#define PY_SSIZE_T_CLEAN
55
#include <Python.h>
66
#include <structmember.h>
7-
// #include <iostream>
7+
#include <iostream>
88
#include "DynamicOneDimensionalArray.hpp"
99
#include "OneDimensionalArray.hpp"
1010
#include "../../../../utils/_backend/cpp/TreeNode.hpp"
@@ -33,67 +33,93 @@ static PyObject* ArrayForTrees___new__(PyTypeObject* type, PyObject *args, PyObj
3333
return reinterpret_cast<PyObject*>(self);
3434
}
3535

36-
// static PyObject* ArrayForTrees__modify(ArrayForTrees *self) {
37-
// if(((double)self->_dynamic_one_dimensional_array->_num/(double)self->_dynamic_one_dimensional_array->_size) < self->_dynamic_one_dimensional_array->_load_factor){
38-
// PyObject* new_indices = PyDict_New();
36+
// In Python, the __str__() of parent class is automatically called.
37+
// Prints the contents of the member dynamic_one_dimensional_array.
38+
static PyObject* ArrayForTrees___str__(ArrayForTrees *self) {
39+
DynamicOneDimensionalArray* doda = self->dynamic_one_dimensional_array;
40+
return DynamicOneDimensionalArray___str__(doda);
41+
}
42+
43+
static PyObject* ArrayForTrees__modify(ArrayForTrees *self) {
44+
DynamicOneDimensionalArray* doda = self->dynamic_one_dimensional_array;
45+
std::cout<<"AFT begins"<<std::endl;
46+
// std::cout<<reinterpret_cast<TreeNode*>(doda->_one_dimensional_array->_data[0])->key<<std::endl;
47+
// std::cout<<reinterpret_cast<TreeNode*>(doda->_one_dimensional_array->_data[0])->data<<std::endl;
3948

40-
// // PyObject* arr_new = OneDimensionalArray___new__(&TreeNodeType, reinterpret_cast<PyObject*>(2*self->_num + 1));
41-
// // This is how arr_new was made in DynamicOneDimensionalArray__modify() for the previous line :-
42-
// long new_size = 2 * self->_dynamic_one_dimensional_array->_num + 1;
43-
// PyObject** arr_new = reinterpret_cast<PyObject**>(std::malloc(new_size * sizeof(PyObject*)));
44-
// for( int i = 0; i < new_size; i++ ) {
45-
// Py_INCREF(Py_None);
46-
// arr_new[i] = Py_None;
47-
// }
49+
if(((double)doda->_num/(double)doda->_size) < doda->_load_factor){
50+
// if(true){
51+
PyObject* new_indices = PyDict_New();
4852

49-
// int j=0;
50-
// PyObject** _data = self->_dynamic_one_dimensional_array->_one_dimensional_array->_data; // Check this line
51-
// for(int i=0; i<=self->_dynamic_one_dimensional_array->_last_pos_filled;i++){
52-
// if(_data[i] != Py_None){ // Check this line. Python code: if self[i] is not None:
53-
// Py_INCREF(Py_None); // This was put in DynamicOneDimensionalArray line 116
54-
// arr_new[j] = _data[i];
55-
// PyObject_SetItem(new_indices, reinterpret_cast<PyObject*>(reinterpret_cast<TreeNode*>(_data[i])->key), reinterpret_cast<PyObject*>(j));
56-
// j += 1;
57-
// }
58-
// }
59-
// for(int i=0;i<j;i++){
60-
// if(reinterpret_cast<TreeNode*>(arr_new[i])->left != Py_None){
61-
// reinterpret_cast<TreeNode*>(arr_new[i])->left = PyObject_GetItem(
62-
// new_indices,
63-
// PyLong_FromLong(
64-
// reinterpret_cast<TreeNode*>(_data[PyLong_AsLong(reinterpret_cast<TreeNode*>(arr_new[i])->left)])->key
65-
// )
66-
// );
67-
// }
68-
// if(reinterpret_cast<TreeNode*>(arr_new[i])->right != Py_None){
69-
// reinterpret_cast<TreeNode*>(arr_new[i])->right = PyObject_GetItem(
70-
// new_indices,
71-
// PyLong_FromLong(
72-
// reinterpret_cast<TreeNode*>(_data[PyLong_AsLong(reinterpret_cast<TreeNode*>(arr_new[i])->right)])->key
73-
// )
74-
// );
75-
// }
76-
// if(reinterpret_cast<TreeNode*>(arr_new[i])->parent != Py_None){
77-
// reinterpret_cast<TreeNode*>(arr_new[i])->parent = PyObject_GetItem(
78-
// new_indices,
79-
// PyLong_FromLong(
80-
// reinterpret_cast<TreeNode*>(_data[PyLong_AsLong(reinterpret_cast<TreeNode*>(arr_new[i])->parent)])->key
81-
// )
82-
// );
83-
// }
84-
// }
85-
// self->_dynamic_one_dimensional_array->_last_pos_filled = j - 1;
86-
// self->_dynamic_one_dimensional_array->_one_dimensional_array->_data = arr_new;
87-
// self->_dynamic_one_dimensional_array->_size = new_size;
88-
// self->_dynamic_one_dimensional_array->_size = new_size;
89-
// return new_indices;
90-
// }
91-
// Py_INCREF(Py_None);
92-
// return Py_None;
93-
// }
53+
// PyObject* arr_new = OneDimensionalArray___new__(&TreeNodeType, reinterpret_cast<PyObject*>(2*self->_num + 1));
54+
// This is how arr_new was made in DynamicOneDimensionalArray__modify() for the previous line :-
55+
std::cout<<"a1"<<std::endl;
56+
long new_size = 2 * doda->_num + 1;
57+
PyObject** arr_new = reinterpret_cast<PyObject**>(std::malloc(new_size * sizeof(PyObject*)));
58+
for( int i = 0; i < new_size; i++ ) {
59+
Py_INCREF(Py_None);
60+
arr_new[i] = Py_None;
61+
}
62+
// return __str__(arr_new, new_size); // Prints: ['', '', '']
63+
std::cout<<"a2"<<std::endl;
64+
int j=0;
65+
PyObject** _data = doda->_one_dimensional_array->_data; // Check this line
66+
for(int i=0; i<=doda->_last_pos_filled;i++){
67+
if(_data[i] != Py_None){ // Check this line. Python code: if self[i] is not None:
68+
Py_INCREF(Py_None); // This was put in DynamicOneDimensionalArray line 116
69+
arr_new[j] = _data[i];
70+
std::cout<<reinterpret_cast<TreeNode*>(_data[i])->key<<std::endl;
71+
PyObject_SetItem(new_indices, reinterpret_cast<PyObject*>(reinterpret_cast<TreeNode*>(_data[i])->key), reinterpret_cast<PyObject*>(j));
72+
j += 1;
73+
}
74+
}
75+
std::cout<<"a3"<<std::endl;
76+
std::cout<<j<<std::endl;
77+
std::cout<<reinterpret_cast<TreeNode*>(arr_new[0])->key<<std::endl;
78+
std::cout<<reinterpret_cast<TreeNode*>(arr_new[0])->data<<std::endl;
79+
for(int i=0;i<j;i++){
80+
if(reinterpret_cast<TreeNode*>(arr_new[i])->left != Py_None){
81+
std::cout<<"here"<<std::endl;
82+
reinterpret_cast<TreeNode*>(arr_new[i])->left = PyObject_GetItem(
83+
new_indices,
84+
PyLong_FromLong(
85+
reinterpret_cast<TreeNode*>(_data[PyLong_AsLong(reinterpret_cast<TreeNode*>(arr_new[i])->left)])->key
86+
)
87+
);
88+
}
89+
std::cout<<"a3.1"<<std::endl;
90+
if(reinterpret_cast<TreeNode*>(arr_new[i])->right != Py_None){
91+
reinterpret_cast<TreeNode*>(arr_new[i])->right = PyObject_GetItem(
92+
new_indices,
93+
PyLong_FromLong(
94+
reinterpret_cast<TreeNode*>(_data[PyLong_AsLong(reinterpret_cast<TreeNode*>(arr_new[i])->right)])->key
95+
)
96+
);
97+
}
98+
std::cout<<"a3.2"<<std::endl;
99+
if(reinterpret_cast<TreeNode*>(arr_new[i])->parent != Py_None){
100+
reinterpret_cast<TreeNode*>(arr_new[i])->parent = PyObject_GetItem(
101+
new_indices,
102+
PyLong_FromLong(
103+
reinterpret_cast<TreeNode*>(_data[PyLong_AsLong(reinterpret_cast<TreeNode*>(arr_new[i])->parent)])->key
104+
)
105+
);
106+
}
107+
std::cout<<"a3.3"<<std::endl;
108+
}
109+
std::cout<<"a4"<<std::endl;
110+
doda->_last_pos_filled = j - 1;
111+
doda->_one_dimensional_array->_data = arr_new;
112+
doda->_size = new_size;
113+
doda->_size = new_size;
114+
std::cout<<"a5"<<std::endl;
115+
return new_indices;
116+
}
117+
Py_INCREF(Py_None);
118+
return Py_None;
119+
}
94120

95121
static struct PyMethodDef ArrayForTrees_PyMethodDef[] = {
96-
// {"_modify", (PyCFunction) ArrayForTrees__modify, METH_NOARGS, NULL},
122+
{"_modify", (PyCFunction) ArrayForTrees__modify, METH_NOARGS, NULL},
97123
{NULL}
98124
};
99125

@@ -120,7 +146,7 @@ static PyTypeObject ArrayForTreesType = {
120146
/* tp_as_mapping */ 0,
121147
/* tp_hash */ 0,
122148
/* tp_call */ 0,
123-
/* tp_str */ 0,
149+
/* tp_str */ (reprfunc) ArrayForTrees___str__,
124150
/* tp_getattro */ 0,
125151
/* tp_setattro */ 0,
126152
/* tp_as_buffer */ 0,

pydatastructs/linear_data_structures/tests/test_arrays.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,18 @@ def test_ArrayForTrees():
138138
AFT = ArrayForTrees
139139
root = TreeNode(1, 100)
140140
A = AFT(TreeNode, [root])
141-
assert str(A[0]) == "(None, 1, 100, None)"
141+
assert str(A) == "['(None, 1, 100, None)']"
142+
A._modify()
142143

143144
def test_cpp_ArrayForTrees():
144145
from pydatastructs.linear_data_structures._backend.cpp import _arrays
145146
AFT = _arrays.ArrayForTrees
146147
root = TreeNode(1, 100)
147148
A = AFT(TreeNode, [root])
148-
# ArrayForTrees has no __str__() method
149+
assert str(A) == "['(None, 1, 100, None)']"
150+
151+
# TO DO: Fix _modify()
152+
# print(A._modify())
153+
154+
# test_ArrayForTrees()
155+
# test_cpp_ArrayForTrees()

0 commit comments

Comments
 (0)