Skip to content

Commit 84f81c1

Browse files
committed
default comparator set in python code and NotImplementedError fixed
1 parent 19175f9 commit 84f81c1

File tree

5 files changed

+22
-14
lines changed

5 files changed

+22
-14
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <Python.h>
66
#include <structmember.h>
77
#include <cstdlib>
8+
#include <iostream>
89
#include "DynamicArray.hpp"
910
#include "OneDimensionalArray.hpp"
1011
#include "../../../../utils/_backend/cpp/utils.hpp"
@@ -25,8 +26,11 @@ static void DynamicOneDimensionalArray_dealloc(DynamicOneDimensionalArray *self)
2526

2627
static PyObject* DynamicOneDimensionalArray___new__(PyTypeObject* type, PyObject *args,
2728
PyObject *kwds) {
29+
std::cout<<"h1"<<std::endl;
2830
DynamicOneDimensionalArray *self;
31+
std::cout<<"h2"<<std::endl;
2932
self = reinterpret_cast<DynamicOneDimensionalArray*>(type->tp_alloc(type, 0));
33+
std::cout<<"h3"<<std::endl;
3034
PyObject* _one_dimensional_array = OneDimensionalArray___new__(&OneDimensionalArrayType, args, kwds);
3135
if( !_one_dimensional_array ) {
3236
return NULL;

pydatastructs/trees/_backend/cpp/BinaryTree.hpp

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,32 +56,31 @@ static PyObject* BinaryTree___new__(PyTypeObject* type, PyObject *args, PyObject
5656
}
5757
// TO DO: Fix the following line!
5858
// self->tree = PyObject_CallMethod(reinterpret_cast<PyObject*>(&ArrayForTreesType),"__new__", "OO", &TreeNodeType, listroot);
59+
5960
self->size = 1;
60-
61-
if(comp == Py_None){
62-
Py_INCREF(Py_None);
63-
self->comparator = Py_None; // set to none for now.
64-
}
65-
else{
66-
self->comparator = comp;
67-
}
61+
// Python code is modified to ensure comp is never None
62+
if (!PyCallable_Check(comp)) {
63+
PyErr_SetString(PyExc_ValueError, "comparator should be callable");
64+
return NULL;
65+
}
66+
self->comparator = comp;
6867
self->is_order_statistic = is_order_statistic;
6968

7069
return reinterpret_cast<PyObject*>(self);
7170
}
7271

7372
static PyObject* BinaryTree_insert(PyTypeObject* type, PyObject *args, PyObject *kwds) {
74-
PyErr_SetString(PyExc_ValueError, "This is an abstract method."); // Currently of type ValueError, change type if needed later
73+
PyErr_SetString(PyExc_NotImplementedError, "This is an abstract method."); // Currently of type ValueError, change type if needed later
7574
return NULL;
7675
}
7776

7877
static PyObject* BinaryTree_delete(PyTypeObject* type, PyObject *args, PyObject *kwds) {
79-
PyErr_SetString(PyExc_ValueError, "This is an abstract method."); // Currently of type ValueError, change type if needed later
78+
PyErr_SetString(PyExc_NotImplementedError, "This is an abstract method."); // Currently of type ValueError, change type if needed later
8079
return NULL;
8180
}
8281

8382
static PyObject* BinaryTree_search(PyTypeObject* type, PyObject *args, PyObject *kwds) {
84-
PyErr_SetString(PyExc_ValueError, "This is an abstract method."); // Currently of type ValueError, change type if needed later
83+
PyErr_SetString(PyExc_NotImplementedError, "This is an abstract method."); // Currently of type ValueError, change type if needed later
8584
return NULL;
8685
}
8786

pydatastructs/trees/binary_trees.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ def __new__(cls, key=None, root_data=None, comp=None,
6161
is_order_statistic=False, **kwargs):
6262
backend = kwargs.get('backend', Backend.PYTHON)
6363
if backend == Backend.CPP:
64+
comp = lambda key1, key2: key1 < key2 \
65+
if comp is None else comp
6466
return _trees.BinaryTree(key, root_data, comp, is_order_statistic, **kwargs) # If any argument is not given, then it is passed as None
6567
obj = object.__new__(cls)
6668
if key is None and root_data is not None:

pydatastructs/trees/tests/test_binary_trees.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,13 @@
99

1010
def test_cpp_BinaryTree():
1111
pass
12-
b = BinaryTree(1,100,backend=Backend.CPP); # Pass None, passing 4 arguments is necessary
12+
b = BinaryTree(1,100,backend=Backend.CPP)
13+
# b.insert() # Correctly throws NotImplementedError: This is an abstract method
14+
# b.delete() # Correctly throws NotImplementedError: This is an abstract method
15+
# b.search() # Correctly throws NotImplementedError: This is an abstract method
1316
# print(str(b))
1417

15-
# test_cpp_BinaryTree()
18+
test_cpp_BinaryTree()
1619

1720
def test_BinarySearchTree():
1821
BST = BinarySearchTree

scripts/build/dummy_submodules_data.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66

77
cpp = 'cpp'
88

9-
dummy_submodules_list = [('_arrays.py', '_algorithms.py'), ('_stack.py',), ('_nodes.py',),('_trees.py',)]
9+
dummy_submodules_list = [('_arrays.py', '_algorithms.py'), ('_stack.py',), ('_nodes.py',), ('_trees.py',)]

0 commit comments

Comments
 (0)