Skip to content

Commit 1c65434

Browse files
committed
Fixed ArrayForTrees__modify() in C++ backend
1 parent 5656f55 commit 1c65434

File tree

2 files changed

+68
-8
lines changed

2 files changed

+68
-8
lines changed

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

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,70 @@ static PyObject* ArrayForTrees___str__(ArrayForTrees *self) {
8787

8888
static PyObject* ArrayForTrees__modify(ArrayForTrees *self,
8989
PyObject* args) {
90-
// TO DO: Write the new modify function here
90+
// This has been tested completely except for the if() statements in the loop mentioned below.
91+
// Returning of the dictionary is also tested and it works fine
92+
93+
if(((double)self->_num/(double)self->_size) < self->_load_factor){
94+
PyObject* new_indices = PyDict_New();
95+
96+
// PyObject* arr_new = OneDimensionalArray___new__(&TreeNodeType, reinterpret_cast<PyObject*>(2*self->_num + 1));
97+
// This is how arr_new was made in DynamicOneDimensionalArray__modify() for the previous line :-
98+
long new_size = 2 * self->_num + 1;
99+
PyObject** arr_new = reinterpret_cast<PyObject**>(std::malloc(new_size * sizeof(PyObject*)));
100+
for( int i = 0; i < new_size; i++ ) {
101+
Py_INCREF(Py_None);
102+
arr_new[i] = Py_None;
103+
}
104+
105+
// For some debugging:
106+
// return __str__(arr_new, new_size); // Prints: ['', '', '']
107+
108+
int j=0;
109+
PyObject** _data = self->_one_dimensional_array->_data; // Check this line
110+
for(int i=0; i<=self->_last_pos_filled;i++){
111+
if(_data[i] != Py_None){ // Check this line. Python code: if self[i] is not None:
112+
Py_INCREF(Py_None); // This was put in DynamicOneDimensionalArray line 116
113+
arr_new[j] = _data[i];
114+
PyObject_SetItem(new_indices, PyLong_FromLong(reinterpret_cast<TreeNode*>(_data[i])->key), PyLong_FromLong(j));
115+
j += 1;
116+
}
117+
}
118+
119+
// The following loop has if() statements which need to be tested
120+
121+
for(int i=0;i<j;i++){
122+
if(reinterpret_cast<TreeNode*>(arr_new[i])->left != Py_None){
123+
reinterpret_cast<TreeNode*>(arr_new[i])->left = PyObject_GetItem(
124+
new_indices,
125+
PyLong_FromLong(
126+
reinterpret_cast<TreeNode*>(_data[PyLong_AsLong(reinterpret_cast<TreeNode*>(arr_new[i])->left)])->key
127+
)
128+
);
129+
}
130+
if(reinterpret_cast<TreeNode*>(arr_new[i])->right != Py_None){
131+
reinterpret_cast<TreeNode*>(arr_new[i])->right = PyObject_GetItem(
132+
new_indices,
133+
PyLong_FromLong(
134+
reinterpret_cast<TreeNode*>(_data[PyLong_AsLong(reinterpret_cast<TreeNode*>(arr_new[i])->right)])->key
135+
)
136+
);
137+
}
138+
if(reinterpret_cast<TreeNode*>(arr_new[i])->parent != Py_None){
139+
reinterpret_cast<TreeNode*>(arr_new[i])->parent = PyObject_GetItem(
140+
new_indices,
141+
PyLong_FromLong(
142+
reinterpret_cast<TreeNode*>(_data[PyLong_AsLong(reinterpret_cast<TreeNode*>(arr_new[i])->parent)])->key
143+
)
144+
);
145+
}
146+
}
147+
self->_last_pos_filled = j - 1;
148+
self->_one_dimensional_array->_data = arr_new;
149+
self->_size = new_size;
150+
self->_size = new_size;
151+
152+
return new_indices;
153+
}
91154
Py_RETURN_NONE;
92155
}
93156

pydatastructs/linear_data_structures/tests/test_arrays.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -139,23 +139,20 @@ def test_ArrayForTrees():
139139
root = TreeNode(1, 100)
140140
A = AFT(TreeNode, [root])
141141
assert str(A) == "['(None, 1, 100, None)']"
142-
print(str(A))
143142
node = TreeNode(2, 200)
144143
A.append(node)
145-
print(str(A))
146-
A._modify()
144+
assert str(A) == "['(None, 1, 100, None)', '(None, 2, 200, None)']"
147145

148146
def test_cpp_ArrayForTrees():
149147
from pydatastructs.linear_data_structures._backend.cpp import _arrays
150148
from pydatastructs.utils._backend.cpp import _nodes
151149
AFT = _arrays.ArrayForTrees
152150
root = TreeNode(1, 100, backend=Backend.CPP)
153151
A = AFT(_nodes.TreeNode, [root])
154-
# print(str(A))
155152
assert str(A) == "['(None, 1, 100, None)']"
156-
157-
# TO DO: Fix _modify()
158-
# print(A._modify())
153+
node = TreeNode(2, 200, backend=Backend.CPP)
154+
A.append(node)
155+
assert str(A) == "['(None, 1, 100, None)', '(None, 2, 200, None)']"
159156

160157
# test_ArrayForTrees()
161158
# test_cpp_ArrayForTrees()

0 commit comments

Comments
 (0)