@@ -87,7 +87,70 @@ static PyObject* ArrayForTrees___str__(ArrayForTrees *self) {
87
87
88
88
static PyObject* ArrayForTrees__modify (ArrayForTrees *self,
89
89
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
+ }
91
154
Py_RETURN_NONE;
92
155
}
93
156
0 commit comments