Skip to content

Commit 2423125

Browse files
committed
Dictionary corrected in ArrayForTrees
1 parent daf2165 commit 2423125

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

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

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,14 @@
44
#define PY_SSIZE_T_CLEAN
55
#include <Python.h>
66
#include <structmember.h>
7-
#include <map>
87
#include "DynamicOneDimensionalArray.hpp"
98
#include "OneDimensionalArray.hpp"
109
#include "../../../../utils/_backend/cpp/TreeNode.hpp"
1110
#include "../../../../utils/_backend/cpp/utils.hpp"
12-
using namespace std;
1311

1412
typedef struct {
1513
PyObject_HEAD
1614
DynamicOneDimensionalArray* _dynamic_one_dimensional_array;
17-
double _load_factor;
18-
long _num;
19-
long _last_pos_filled;
20-
long _size;
21-
PyObject* _dtype;
2215
} ArrayForTrees;
2316

2417
static void ArrayForTrees_dealloc(ArrayForTrees *self) {
@@ -28,7 +21,7 @@ static void ArrayForTrees_dealloc(ArrayForTrees *self) {
2821

2922
static PyObject* ArrayForTrees__modify(ArrayForTrees *self) {
3023
if(((double)self->_num/(double)self->_size) < self->_load_factor){
31-
map<long , long> new_indices;
24+
PyObject* new_indices = PyDict_New();
3225

3326
// PyObject* arr_new = OneDimensionalArray___new__(&TreeNodeType, reinterpret_cast<PyObject*>(2*self->_num + 1));
3427
// This is how arr_new was made in DynamicOneDimensionalArray__modify() for the previous line :-
@@ -45,26 +38,41 @@ static PyObject* ArrayForTrees__modify(ArrayForTrees *self) {
4538
if(_data[i] != Py_None){ // Check this line. Python code: if self[i] is not None:
4639
Py_INCREF(Py_None); // This was put in DynamicOneDimensionalArray line 116
4740
arr_new[j] = _data[i];
48-
new_indices[reinterpret_cast<TreeNode*>(_data[i])->key] = j; // Other nodes are also child classes of TreeNode
41+
PyObject_SetItem(new_indices, reinterpret_cast<TreeNode*>(_data[i])->key, j);
4942
j += 1;
5043
}
5144
}
5245
for(int i=0;i<j;i++){
5346
if(reinterpret_cast<TreeNode*>(arr_new[i])->left != Py_None){
54-
reinterpret_cast<TreeNode*>(arr_new[i])->left = reinterpret_cast<PyObject*>(new_indices[reinterpret_cast<TreeNode*>(_data[reinterpret_cast<long>(reinterpret_cast<TreeNode*>(arr_new[i])->left)])->key]);
47+
reinterpret_cast<TreeNode*>(arr_new[i])->left = PyObject_GetItem(
48+
new_indices,
49+
PyLong_FromLong(
50+
reinterpret_cast<TreeNode*>(_data[PyLong_AsLong(reinterpret_cast<TreeNode*>(arr_new[i])->left)])->key
51+
)
52+
);
5553
}
5654
if(reinterpret_cast<TreeNode*>(arr_new[i])->right != Py_None){
57-
reinterpret_cast<TreeNode*>(arr_new[i])->right = reinterpret_cast<PyObject*>(new_indices[reinterpret_cast<TreeNode*>(_data[reinterpret_cast<long>(reinterpret_cast<TreeNode*>(arr_new[i])->right)])->key]);
55+
reinterpret_cast<TreeNode*>(arr_new[i])->right = PyObject_GetItem(
56+
new_indices,
57+
PyLong_FromLong(
58+
reinterpret_cast<TreeNode*>(_data[PyLong_AsLong(reinterpret_cast<TreeNode*>(arr_new[i])->right)])->key
59+
)
60+
);
5861
}
5962
if(reinterpret_cast<TreeNode*>(arr_new[i])->parent != Py_None){
60-
reinterpret_cast<TreeNode*>(arr_new[i])->parent = reinterpret_cast<PyObject*>(new_indices[reinterpret_cast<TreeNode*>(_data[reinterpret_cast<long>(reinterpret_cast<TreeNode*>(arr_new[i])->parent)])->key]);
63+
reinterpret_cast<TreeNode*>(arr_new[i])->parent = PyObject_GetItem(
64+
new_indices,
65+
PyLong_FromLong(
66+
reinterpret_cast<TreeNode*>(_data[PyLong_AsLong(reinterpret_cast<TreeNode*>(arr_new[i])->parent)])->key
67+
)
68+
);
6169
}
6270
}
6371
self->_last_pos_filled = j - 1;
6472
self->_dynamic_one_dimensional_array->_one_dimensional_array->_data = arr_new;
6573
self->_dynamic_one_dimensional_array->_size = new_size;
6674
self->_size = new_size;
67-
return reinterpret_cast<PyObject*>(new_indices); // Works except for this
75+
return new_indices;
6876
}
6977
Py_INCREF(Py_None);
7078
return Py_None;

0 commit comments

Comments
 (0)