4
4
#define PY_SSIZE_T_CLEAN
5
5
#include < Python.h>
6
6
#include < structmember.h>
7
- // #include <iostream>
7
+ #include < iostream>
8
8
#include " DynamicOneDimensionalArray.hpp"
9
9
#include " OneDimensionalArray.hpp"
10
10
#include " ../../../../utils/_backend/cpp/TreeNode.hpp"
@@ -33,67 +33,93 @@ static PyObject* ArrayForTrees___new__(PyTypeObject* type, PyObject *args, PyObj
33
33
return reinterpret_cast <PyObject*>(self);
34
34
}
35
35
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;
39
48
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 ();
48
52
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
+ }
94
120
95
121
static struct PyMethodDef ArrayForTrees_PyMethodDef[] = {
96
- // {"_modify", (PyCFunction) ArrayForTrees__modify, METH_NOARGS, NULL},
122
+ {" _modify" , (PyCFunction) ArrayForTrees__modify, METH_NOARGS, NULL },
97
123
{NULL }
98
124
};
99
125
@@ -120,7 +146,7 @@ static PyTypeObject ArrayForTreesType = {
120
146
/* tp_as_mapping */ 0 ,
121
147
/* tp_hash */ 0 ,
122
148
/* tp_call */ 0 ,
123
- /* tp_str */ 0 ,
149
+ /* tp_str */ (reprfunc) ArrayForTrees___str__ ,
124
150
/* tp_getattro */ 0 ,
125
151
/* tp_setattro */ 0 ,
126
152
/* tp_as_buffer */ 0 ,
0 commit comments