4
4
#define PY_SSIZE_T_CLEAN
5
5
#include < Python.h>
6
6
#include < structmember.h>
7
- #include < map>
8
7
#include " DynamicOneDimensionalArray.hpp"
9
8
#include " OneDimensionalArray.hpp"
10
9
#include " ../../../../utils/_backend/cpp/TreeNode.hpp"
11
10
#include " ../../../../utils/_backend/cpp/utils.hpp"
12
- using namespace std ;
13
11
14
12
typedef struct {
15
13
PyObject_HEAD
16
14
DynamicOneDimensionalArray* _dynamic_one_dimensional_array;
17
- double _load_factor;
18
- long _num;
19
- long _last_pos_filled;
20
- long _size;
21
- PyObject* _dtype;
22
15
} ArrayForTrees;
23
16
24
17
static void ArrayForTrees_dealloc (ArrayForTrees *self) {
@@ -28,7 +21,7 @@ static void ArrayForTrees_dealloc(ArrayForTrees *self) {
28
21
29
22
static PyObject* ArrayForTrees__modify (ArrayForTrees *self) {
30
23
if (((double )self->_num /(double )self->_size ) < self->_load_factor ){
31
- map< long , long > new_indices ;
24
+ PyObject* new_indices = PyDict_New () ;
32
25
33
26
// PyObject* arr_new = OneDimensionalArray___new__(&TreeNodeType, reinterpret_cast<PyObject*>(2*self->_num + 1));
34
27
// This is how arr_new was made in DynamicOneDimensionalArray__modify() for the previous line :-
@@ -45,26 +38,41 @@ static PyObject* ArrayForTrees__modify(ArrayForTrees *self) {
45
38
if (_data[i] != Py_None){ // Check this line. Python code: if self[i] is not None:
46
39
Py_INCREF (Py_None); // This was put in DynamicOneDimensionalArray line 116
47
40
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);
49
42
j += 1 ;
50
43
}
51
44
}
52
45
for (int i=0 ;i<j;i++){
53
46
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
+ );
55
53
}
56
54
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
+ );
58
61
}
59
62
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
+ );
61
69
}
62
70
}
63
71
self->_last_pos_filled = j - 1 ;
64
72
self->_dynamic_one_dimensional_array ->_one_dimensional_array ->_data = arr_new;
65
73
self->_dynamic_one_dimensional_array ->_size = new_size;
66
74
self->_size = new_size;
67
- return reinterpret_cast <PyObject*>( new_indices); // Works except for this
75
+ return new_indices;
68
76
}
69
77
Py_INCREF (Py_None);
70
78
return Py_None;
0 commit comments