Skip to content

Commit c8697a7

Browse files
committed
PR ready: improved code quality, removed unnecessary comments
1 parent 1b2d32d commit c8697a7

File tree

12 files changed

+18
-73
lines changed

12 files changed

+18
-73
lines changed

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

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
#include <Python.h>
66
#include <structmember.h>
77
#include <cstdlib>
8-
#include <iostream>
9-
#include "DynamicOneDimensionalArray.hpp" // See if this is needed
108
#include "OneDimensionalArray.hpp"
119
#include "DynamicArray.hpp"
1210
#include "../../../../utils/_backend/cpp/TreeNode.hpp"
@@ -87,37 +85,28 @@ static PyObject* ArrayForTrees___str__(ArrayForTrees *self) {
8785

8886
static PyObject* ArrayForTrees__modify(ArrayForTrees *self,
8987
PyObject* args) {
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
9288

9389
if (((double)self->_num/(double)self->_size) < self->_load_factor) {
9490
PyObject* new_indices = PyDict_New();
9591

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 :-
9892
long new_size = 2 * self->_num + 1;
9993
PyObject** arr_new = reinterpret_cast<PyObject**>(std::malloc(new_size * sizeof(PyObject*)));
10094
for( int i = 0; i < new_size; i++ ) {
10195
Py_INCREF(Py_None);
10296
arr_new[i] = Py_None;
10397
}
10498

105-
// For some debugging:
106-
// return __str__(arr_new, new_size); // Prints: ['', '', '']
107-
10899
int j=0;
109-
PyObject** _data = self->_one_dimensional_array->_data; // Check this line
100+
PyObject** _data = self->_one_dimensional_array->_data;
110101
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
102+
if (_data[i] != Py_None) {
103+
Py_INCREF(Py_None);
113104
arr_new[j] = _data[i];
114105
PyObject_SetItem(new_indices, reinterpret_cast<TreeNode*>(_data[i])->key, PyLong_FromLong(j));
115106
j += 1;
116107
}
117108
}
118109

119-
// The following loop has if () statements which need to be tested
120-
121110
for(int i=0;i<j;i++) {
122111
if (reinterpret_cast<TreeNode*>(arr_new[i])->left != Py_None) {
123112
reinterpret_cast<TreeNode*>(arr_new[i])->left = PyObject_GetItem(

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#include <Python.h>
66
#include <structmember.h>
77
#include <cstdlib>
8-
// #include <iostream>
98
#include "DynamicArray.hpp"
109
#include "OneDimensionalArray.hpp"
1110
#include "../../../../utils/_backend/cpp/utils.hpp"

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#define PY_SSIZE_T_CLEAN
55
#include <Python.h>
66
#include <structmember.h>
7-
// #include <iostream>
87
#include <cstdlib>
98
#include "Array.hpp"
109
#include "../../../../utils/_backend/cpp/utils.hpp"

pydatastructs/linear_data_structures/arrays.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ def append(self, el):
407407
self[self._last_pos_filled + 1] = el
408408
self._last_pos_filled += 1
409409
self._num += 1
410-
self._modify() #if self is ArrayForTrees, then that _modify() is called
410+
self._modify()
411411

412412
def delete(self, idx):
413413
if idx <= self._last_pos_filled and idx >= 0 and \
@@ -416,7 +416,7 @@ def delete(self, idx):
416416
self._num -= 1
417417
if self._last_pos_filled == idx:
418418
self._last_pos_filled -= 1
419-
# self._size -= 1 # Size of array
419+
# self._size -= 1 # Check if size of array should be changed
420420
return self._modify()
421421

422422
@property

pydatastructs/linear_data_structures/tests/test_arrays.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ def test_DynamicOneDimensionalArray2():
134134
A = DODA(TreeNode, [root])
135135
assert str(A[0]) == "(None, 1, 100, None)"
136136

137+
# To Do: Fix this size issue:
138+
137139
# def test_DynamicOneDimensionalArray3():
138140
# DODA = DynamicOneDimensionalArray
139141
# A = DODA(int, 1)
@@ -147,10 +149,6 @@ def test_DynamicOneDimensionalArray2():
147149
# print(str(A))
148150
# print(A.size)
149151

150-
# test_DynamicOneDimensionalArray()
151-
# test_DynamicOneDimensionalArray2()
152-
# test_DynamicOneDimensionalArray3()
153-
154152
def test_ArrayForTrees():
155153
AFT = ArrayForTrees
156154
root = TreeNode(1, 100)
@@ -170,6 +168,3 @@ def test_cpp_ArrayForTrees():
170168
node = TreeNode(2, 200, backend=Backend.CPP)
171169
A.append(node)
172170
assert str(A) == "['(None, 1, 100, None)', '(None, 2, 200, None)']"
173-
174-
# test_ArrayForTrees()
175-
# test_cpp_ArrayForTrees()

pydatastructs/trees/_backend/cpp/BinarySearchTree.hpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#define PY_SSIZE_T_CLEAN
55
#include <Python.h>
66
#include <structmember.h>
7-
#include <iostream>
87
#include <cstdlib>
98
#include <stack>
109
#include "../../../utils/_backend/cpp/utils.hpp"
@@ -68,7 +67,7 @@ static PyObject* BinarySearchTree__update_size(BinarySearchTree* self, PyObject
6867
while (walk!=Py_None) {
6968
TreeNode* node = reinterpret_cast<TreeNode*>(self->binary_tree->tree->_one_dimensional_array->_data[PyLong_AsLong(walk)]);
7069
node->size = BinarySearchTree_left_size(self, node) + BinarySearchTree_right_size(self, node) + 1;
71-
walk = node->parent; // Parent is a long or a Py_None, hence, it is a PyObject
70+
walk = node->parent;
7271
}
7372
}
7473
Py_RETURN_NONE;
@@ -84,7 +83,7 @@ static PyObject* BinarySearchTree_search(BinarySearchTree* self, PyObject* args,
8483
}
8584
BinaryTree* bt = self->binary_tree;
8685
PyObject* parent = Py_None;
87-
PyObject* walk = PyLong_FromLong(PyLong_AsLong(bt->root_idx)); // root_idx is size_t, change it to long if needed
86+
PyObject* walk = PyLong_FromLong(PyLong_AsLong(bt->root_idx));
8887

8988
if (reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(walk)])->key == Py_None) {
9089
Py_RETURN_NONE;
@@ -98,7 +97,6 @@ static PyObject* BinarySearchTree_search(BinarySearchTree* self, PyObject* args,
9897
}
9998
parent = walk;
10099

101-
// The comparator has been tested. It works fine. :)
102100
if (!PyCallable_Check(bt->comparator)) {
103101
PyErr_SetString(PyExc_ValueError, "comparator should be callable");
104102
return NULL;
@@ -242,7 +240,6 @@ static PyObject* BinarySearchTree_delete(BinarySearchTree* self, PyObject *args,
242240
PyObject* par_key = reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(parent)])->key;
243241
PyObject* root_key = reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(bt->root_idx)])->key;
244242
PyObject* new_indices = ArrayForTrees_delete(bt->tree, Py_BuildValue("(O)",walk));
245-
// bt->size = bt->size - 1; // TO DO: Fix b.insert(12,12), b.delete(12), b.insert(12)
246243
if (new_indices != Py_None) {
247244
a = PyDict_GetItem(new_indices, par_key);
248245
bt->root_idx = PyDict_GetItem(new_indices, root_key);
@@ -656,7 +653,7 @@ static PyObject* BinarySearchTree_select(BinarySearchTree* self, PyObject* args)
656653
i = i - (l + 1);
657654
}
658655
}
659-
Py_RETURN_NONE; // This is a dummy return
656+
Py_RETURN_NONE; // dummy return statement, never executed
660657
}
661658

662659
static struct PyMethodDef BinarySearchTree_PyMethodDef[] = {

pydatastructs/trees/_backend/cpp/BinaryTree.hpp

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
#define PY_SSIZE_T_CLEAN
55
#include <Python.h>
6-
// #include <iostream>
76
#include <structmember.h>
87
#include <cstdlib>
98
#include "../../../utils/_backend/cpp/utils.hpp"
@@ -28,9 +27,6 @@ static PyObject* BinaryTree___new__(PyTypeObject* type, PyObject *args, PyObject
2827
BinaryTree *self;
2928
self = reinterpret_cast<BinaryTree*>(type->tp_alloc(type, 0));
3029

31-
// // Check what this is: (python code below:)
32-
// // obj = object.__new__(cls)
33-
3430
// Assume that arguments are in the order below. Modify the python code such that this is true (ie; pass None for other arguments)
3531
PyObject *key = PyObject_GetItem(args, PyZero);
3632
PyObject *root_data = PyObject_GetItem(args, PyOne);
@@ -41,7 +37,7 @@ static PyObject* BinaryTree___new__(PyTypeObject* type, PyObject *args, PyObject
4137
return NULL;
4238
}
4339
Py_INCREF(Py_None);
44-
key = root_data == Py_None ? Py_None : key; // This key is the argument, not self->key
40+
key = root_data == Py_None ? Py_None : key;
4541

4642
if (PyType_Ready(&TreeNodeType) < 0) { // This has to be present to finalize a type object. This should be called on all type objects to finish their initialization.
4743
return NULL;
@@ -61,10 +57,6 @@ static PyObject* BinaryTree___new__(PyTypeObject* type, PyObject *args, PyObject
6157
return NULL;
6258
}
6359

64-
// Don't delete these 2 lines, keep these for reference:
65-
// ArrayForTrees* p = reinterpret_cast<ArrayForTrees*>(PyObject_CallMethod(reinterpret_cast<PyObject*>(&ArrayForTreesType),"__new__", "OOO", &DynamicOneDimensionalArrayType, &TreeNodeType, listroot));
66-
// DynamicOneDimensionalArray* p = reinterpret_cast<DynamicOneDimensionalArray*>(DynamicOneDimensionalArray___new__(&DynamicOneDimensionalArrayType, args2, kwds));
67-
6860
Py_INCREF(Py_None);
6961
PyObject* args2 = Py_BuildValue("(OO)", &TreeNodeType, listroot);
7062
PyObject* kwds2 = Py_BuildValue("()");
@@ -82,7 +74,7 @@ static PyObject* BinaryTree___new__(PyTypeObject* type, PyObject *args, PyObject
8274
PyErr_SetString(PyExc_ValueError, "comparator should be callable");
8375
return NULL;
8476
}
85-
self->comparator = comp; // Comparator has been tested. It works fine!
77+
self->comparator = comp;
8678
self->is_order_statistic = PyLong_AsLong(is_order_statistic);
8779

8880
return reinterpret_cast<PyObject*>(self);
@@ -119,7 +111,7 @@ static PyObject* BinaryTree___str__(BinaryTree *self) {
119111
PyList_SET_ITEM(list, i, empty_string);
120112
}
121113
}
122-
return PyObject_Str(list); // use this or __str()__ (that is defined in utils)?
114+
return PyObject_Str(list);
123115
}
124116

125117
static struct PyMethodDef BinaryTree_PyMethodDef[] = {
@@ -129,7 +121,6 @@ static struct PyMethodDef BinaryTree_PyMethodDef[] = {
129121
{NULL}
130122
};
131123

132-
// Check if PyMemberDef is actually needed:
133124
static PyMemberDef BinaryTree_PyMemberDef[] = {
134125
{"root_idx", T_OBJECT, offsetof(BinaryTree, root_idx), READONLY, "Index of the root node"},
135126
{"comparator", T_OBJECT, offsetof(BinaryTree, comparator), 0, "Comparator function"},

pydatastructs/trees/tests/test_binary_trees.py

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@ def cust_comp(a,b):
1111
print("custom comparator called")
1212
return a<b
1313

14+
################### C++ Backend Tests below ###################
15+
1416
def test_cpp_BinaryTree():
15-
# b = BinaryTree(1,100,comp=cust_comp,backend=Backend.CPP)
17+
# b = BinaryTree(1,100,comp=cust_comp,backend=Backend.CPP) # This is how a custom comparator can be passed
1618
b = BinaryTree(1,100,backend=Backend.CPP)
1719
assert raises(NotImplementedError, b.insert) # Correctly throws NotImplementedError: This is an abstract method
1820
assert raises(NotImplementedError, b.delete) # Correctly throws NotImplementedError: This is an abstract method
1921
assert raises(NotImplementedError, b.search) # Correctly throws NotImplementedError: This is an abstract method
20-
# print(str(b))
2122
assert str(b) == "[(None, 1, 100, None)]"
2223

2324
# test_cpp_BinaryTree()
@@ -45,7 +46,6 @@ def test_cpp_BST1():
4546
def test_cpp_BST2():
4647
BST = BinarySearchTree
4748
b = BST(8, 8, backend=Backend.CPP)
48-
# b = BST(8, 8)
4949

5050
##### insert() and delete() tests #####
5151
b.delete(8)
@@ -90,12 +90,10 @@ def test_cpp_BST2():
9090
assert str(b) == "[(None, 8, 8, 2), '', (None, 14, 14, None)]"
9191

9292
bc = BST(1, 1, backend=Backend.CPP)
93-
# bc = BST(1, 1)
9493
assert bc.insert(1, 2) is None
9594
assert bc.delete(1, balancing_info=True) is None
9695

9796
b2 = BST(-8, 8, backend=Backend.CPP)
98-
# b2 = BST(-8, 8)
9997
b2.insert(-3, 3)
10098
b2.insert(-10, 10)
10199
b2.insert(-1, 1)
@@ -105,7 +103,6 @@ def test_cpp_BST2():
105103
b2.insert(-14, 14)
106104
b2.insert(-13, 13)
107105
assert str(b2) == "[(2, -8, 8, 1), (4, -3, 3, 3), (7, -10, 10, None), (None, -1, 1, None), (6, -6, 6, 5), (None, -4, 4, None), (None, -7, 7, None), (None, -14, 14, 8), (None, -13, 13, None)]"
108-
# To Do: Fix CI error in next 5 lines, if we use: assert b2.delete(-13) is True
109106
b2.delete(-13)
110107
assert str(b2) == "[(2, -8, 8, 1), (4, -3, 3, 3), (7, -10, 10, None), (None, -1, 1, None), (6, -6, 6, 5), (None, -4, 4, None), (None, -7, 7, None), (None, -14, 14, None)]"
111108
b2.delete(-10)
@@ -114,7 +111,6 @@ def test_cpp_BST2():
114111
assert str(b2) == "[(7, -8, 8, 1), (4, -1, 1, None), '', '', (6, -6, 6, 5), (None, -4, 4, None), (None, -7, 7, None), (None, -14, 14, None)]"
115112

116113
bl1 = BST(backend=Backend.CPP)
117-
# bl1 = BST()
118114
nodes = [50, 30, 90, 70, 100, 60, 80, 55, 20, 40, 15, 10, 16, 17, 18]
119115
for node in nodes:
120116
bl1.insert(node, node)
@@ -136,7 +132,6 @@ def test_cpp_BST2():
136132
assert raises(ValueError, lambda: bl1.lowest_common_ancestor(-3, 4, 2))
137133

138134
bl2 = BST(backend=Backend.CPP)
139-
# bl2 = BST()
140135
nodes = [50, 30, 90, 70, 100, 60, 80, 55, 20, 40, 15, 10, 16, 17, 18]
141136
for node in nodes:
142137
bl2.insert(node, node)
@@ -171,7 +166,6 @@ def test_cpp_BST2():
171166
assert bl2.select(i+1).key == select_list[i]
172167

173168
b3 = BST(backend=Backend.CPP)
174-
# b3 = BST()
175169
b3.insert(10, 10)
176170
b3.insert(18, 18)
177171
b3.insert(7, 7)
@@ -188,7 +182,6 @@ def test_cpp_BST2():
188182
assert b3.lower_bound(-1) == 7
189183
assert b3.lower_bound(20) is None
190184

191-
# test_cpp_BST2()
192185

193186
def test_cpp_BST_speed():
194187
BST = BinarySearchTree
@@ -206,7 +199,6 @@ def test_cpp_BST_speed():
206199
print("Time taken by Python backend: ",t2-t1,"s")
207200
print("Time taken by C++ backend: ",t4-t3,"s")
208201

209-
# test_cpp_BST_speed()
210202

211203
################### Python Tests below ###################
212204

@@ -309,7 +301,6 @@ def test_BinarySearchTree():
309301
assert raises(ValueError, lambda: bl.lowest_common_ancestor(200, 60, 1))
310302
assert raises(ValueError, lambda: bl.lowest_common_ancestor(-3, 4, 1))
311303

312-
# test_BinarySearchTree()
313304

314305
def test_BinaryTreeTraversal():
315306
BST = BinarySearchTree
@@ -546,7 +537,6 @@ def test_select_rank(expected_output):
546537
a5.delete(2)
547538
test_select_rank([])
548539

549-
# test_AVLTree()
550540

551541
def test_BinaryIndexedTree():
552542

@@ -562,7 +552,6 @@ def test_BinaryIndexedTree():
562552
assert t.get_sum(0, 4) == 114
563553
assert t.get_sum(1, 9) == 54
564554

565-
# test_BinaryIndexedTree()
566555

567556
def test_CartesianTree():
568557
tree = CartesianTree()

pydatastructs/utils/_backend/cpp/Node.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ static void Node_dealloc(Node *self) {
1515
Py_TYPE(self)->tp_free(reinterpret_cast<PyObject*>(self));
1616
}
1717

18-
// Class Node has no functions, not even a __new()__ function
1918

2019
static PyTypeObject NodeType = {
2120
/* tp_name */ PyVarObject_HEAD_INIT(NULL, 0) "Node",

pydatastructs/utils/_backend/cpp/TreeNode.hpp

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,14 @@ typedef struct {
2020
} TreeNode;
2121

2222
static void TreeNode_dealloc(TreeNode *self) {
23-
// left and right are long values, no need to dealloc for them
24-
// TreeNode_dealloc(reinterpret_cast<TreeNode*>(TreeNode->left));
25-
// TreeNode_dealloc(reinterpret_cast<TreeNode*>(TreeNode->right));
26-
// Check if other deallocs are needed using Py_XDECREF
2723
Py_TYPE(self)->tp_free(reinterpret_cast<PyObject*>(self));
2824
}
2925

3026
static PyObject* TreeNode___new__(PyTypeObject* type, PyObject *args, PyObject *kwds) {
3127
TreeNode *self;
3228
self = reinterpret_cast<TreeNode*>(type->tp_alloc(type, 0));
3329

34-
// Check what this is: (python code below:)
35-
// obj = Node.__new__(cls)
36-
37-
// Assume that arguments are in the order below. Modify the code such that this is true.
30+
// Assume that arguments are in the order below. Python code is such that this is true.
3831
self->key = PyObject_GetItem(args, PyZero);
3932
self->data = PyObject_GetItem(args, PyOne);
4033

@@ -69,6 +62,7 @@ static struct PyMemberDef TreeNode_PyMemberDef[] = {
6962
{NULL},
7063
};
7164

65+
7266
static PyTypeObject TreeNodeType = {
7367
/* tp_name */ PyVarObject_HEAD_INIT(NULL, 0) "TreeNode",
7468
/* tp_basicsize */ sizeof(TreeNode),

0 commit comments

Comments
 (0)