Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C++ backend for Splay Trees #562

Merged
merged 16 commits into from
Jun 27, 2024
4 changes: 2 additions & 2 deletions pydatastructs/trees/_backend/cpp/BinaryIndexedTree.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ static PyObject* BinaryIndexedTree___new__(PyTypeObject* type, PyObject *args, P
}
self->array = reinterpret_cast<OneDimensionalArray*>(_one_dimensional_array);
self->tree = PyList_New(self->array->_size+2);
for(int i=0;i<self->array->_size+2;i++){
for(int i=0;i<self->array->_size+2;i++) {
PyList_SetItem(self->tree, i, PyZero);
}
self->flag = PyList_New(self->array->_size);
for(int i=0;i<self->array->_size;i++){
for(int i=0;i<self->array->_size;i++) {
PyList_SetItem(self->flag, i, PyZero);
BinaryIndexedTree_update(self, Py_BuildValue("(OO)", PyLong_FromLong(i), self->array->_data[i]));
}
Expand Down
20 changes: 10 additions & 10 deletions pydatastructs/trees/_backend/cpp/BinarySearchTree.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ static PyObject* BinarySearchTree_search(BinarySearchTree* self, PyObject* args,
PyErr_SetString(PyExc_ValueError, "comparator should be callable");
return NULL;
}
PyObject* arguments = Py_BuildValue("OO", key, curr_key);
PyObject* arguments = Py_BuildValue("(OO)", key, curr_key);
PyObject* res = PyObject_CallObject(bt->comparator, arguments);
Py_DECREF(arguments);
if (!PyLong_Check(res)) {
Expand All @@ -125,7 +125,7 @@ static PyObject* BinarySearchTree_search(BinarySearchTree* self, PyObject* args,
return walk;
}
else {
return Py_BuildValue("OO",walk,parent);
return Py_BuildValue("(OO)",walk,parent);
}
Py_RETURN_NONE; // dummy return statement, never executed
}
Expand Down Expand Up @@ -168,7 +168,7 @@ static PyObject* BinarySearchTree_insert(BinarySearchTree* self, PyObject* args)
PyErr_SetString(PyExc_ValueError, "comparator should be callable");
return NULL;
}
PyObject* arguments = Py_BuildValue("OO", key, curr_key);
PyObject* arguments = Py_BuildValue("(OO)", key, curr_key);
PyObject* cres = PyObject_CallObject(bt->comparator, arguments);
Py_DECREF(arguments);
if (!PyLong_Check(cres)) {
Expand Down Expand Up @@ -359,7 +359,7 @@ static PyObject* BinarySearchTree__bound_helper(BinarySearchTree* self, PyObject
PyErr_SetString(PyExc_ValueError, "comparator should be callable");
return NULL;
}
PyObject* arguments = Py_BuildValue("OO", reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(node_idx)])->key, bound_key);
PyObject* arguments = Py_BuildValue("(OO)", reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(node_idx)])->key, bound_key);
PyObject* cres = PyObject_CallObject(bt->comparator, arguments);
Py_DECREF(arguments);
if (!PyLong_Check(cres)) {
Expand Down Expand Up @@ -481,7 +481,7 @@ static PyObject* BinarySearchTree__lca_2(BinarySearchTree* self, PyObject* args)
PyErr_SetString(PyExc_ValueError, "comparator should be callable");
return NULL;
}
PyObject* arguments1 = Py_BuildValue("OO", reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(u)])->key, reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(curr_root)])->key);
PyObject* arguments1 = Py_BuildValue("(OO)", reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(u)])->key, reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(curr_root)])->key);
PyObject* cres1 = PyObject_CallObject(bt->comparator, arguments1);
Py_DECREF(arguments1);
if (!PyLong_Check(cres1)) {
Expand All @@ -494,7 +494,7 @@ static PyObject* BinarySearchTree__lca_2(BinarySearchTree* self, PyObject* args)
PyErr_SetString(PyExc_ValueError, "comparator should be callable");
return NULL;
}
PyObject* arguments2 = Py_BuildValue("OO", reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(v)])->key, reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(curr_root)])->key);
PyObject* arguments2 = Py_BuildValue("(OO)", reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(v)])->key, reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(curr_root)])->key);
PyObject* cres2 = PyObject_CallObject(bt->comparator, arguments2);
Py_DECREF(arguments2);
if (!PyLong_Check(cres2)) {
Expand Down Expand Up @@ -522,7 +522,7 @@ static PyObject* BinarySearchTree__lca_2(BinarySearchTree* self, PyObject* args)
PyErr_SetString(PyExc_ValueError, "comparator should be callable");
return NULL;
}
PyObject* arguments1 = Py_BuildValue("OO", reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(u)])->key, reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(curr_root)])->key);
PyObject* arguments1 = Py_BuildValue("(OO)", reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(u)])->key, reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(curr_root)])->key);
PyObject* cres1 = PyObject_CallObject(bt->comparator, arguments1);
Py_DECREF(arguments1);
if (!PyLong_Check(cres1)) {
Expand All @@ -535,7 +535,7 @@ static PyObject* BinarySearchTree__lca_2(BinarySearchTree* self, PyObject* args)
PyErr_SetString(PyExc_ValueError, "comparator should be callable");
return NULL;
}
PyObject* arguments2 = Py_BuildValue("OO", reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(v)])->key, reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(curr_root)])->key);
PyObject* arguments2 = Py_BuildValue("(OO)", reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(v)])->key, reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(curr_root)])->key);
PyObject* cres2 = PyObject_CallObject(bt->comparator, arguments2);
Py_DECREF(arguments2);
if (!PyLong_Check(cres2)) {
Expand Down Expand Up @@ -616,7 +616,7 @@ static PyObject* BinarySearchTree_select(BinarySearchTree* self, PyObject* args)
PyErr_SetString(PyExc_ValueError, "comparator should be callable");
return NULL;
}
PyObject* arguments = Py_BuildValue("OO", reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(left_walk)])->key, reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(walk)])->key);
PyObject* arguments = Py_BuildValue("(OO)", reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(left_walk)])->key, reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(walk)])->key);
PyObject* cres = PyObject_CallObject(bt->comparator, arguments);
Py_DECREF(arguments);
if (!PyLong_Check(cres)) {
Expand All @@ -637,7 +637,7 @@ static PyObject* BinarySearchTree_select(BinarySearchTree* self, PyObject* args)
PyErr_SetString(PyExc_ValueError, "comparator should be callable");
return NULL;
}
PyObject* arguments = Py_BuildValue("OO", reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(right_walk)])->key, reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(walk)])->key);
PyObject* arguments = Py_BuildValue("(OO)", reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(right_walk)])->key, reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(walk)])->key);
PyObject* cres = PyObject_CallObject(bt->comparator, arguments);
Py_DECREF(arguments);
if (!PyLong_Check(cres)) {
Expand Down
4 changes: 2 additions & 2 deletions pydatastructs/trees/_backend/cpp/BinaryTree.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,10 @@ static struct PyMethodDef BinaryTree_PyMethodDef[] = {
};

static PyMemberDef BinaryTree_PyMemberDef[] = {
{"root_idx", T_OBJECT, offsetof(BinaryTree, root_idx), READONLY, "Index of the root node"},
{"root_idx", T_OBJECT_EX, offsetof(BinaryTree, root_idx), 0, "Index of the root node"},
{"comparator", T_OBJECT, offsetof(BinaryTree, comparator), 0, "Comparator function"},
{"tree", T_OBJECT_EX, offsetof(BinaryTree, tree), 0, "Tree"},
{"size", T_LONG, offsetof(BinaryTree, size), READONLY, "Size of the tree"},
{"size", T_LONG, offsetof(BinaryTree, size), 0, "Size of the tree"},
{"is_order_statistic", T_LONG, offsetof(BinaryTree, is_order_statistic), 0, "Whether the tree is ordered statically or not"},
{NULL} /* Sentinel */
};
Expand Down
9 changes: 8 additions & 1 deletion pydatastructs/trees/_backend/cpp/BinaryTreeTraversal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "BinarySearchTree.hpp"
#include "SelfBalancingBinaryTree.hpp"
#include "RedBlackTree.hpp"
#include "SplayTree.hpp"

typedef struct {
PyObject_HEAD
Expand All @@ -40,8 +41,14 @@ static PyObject* BinaryTreeTraversal___new__(PyTypeObject* type, PyObject *args,
if (PyType_Ready(&RedBlackTreeType) < 0) { // This has to be present to finalize a type object. This should be called on all type objects to finish their initialization.
return NULL;
}
if (PyType_Ready(&SplayTreeType) < 0) { // This has to be present to finalize a type object. This should be called on all type objects to finish their initialization.
return NULL;
}

if (PyObject_IsInstance(tree, (PyObject *)&RedBlackTreeType)) {
if (PyObject_IsInstance(tree, (PyObject *)&SplayTreeType)) {
self->tree = reinterpret_cast<SplayTree*>(tree)->sbbt->bst->binary_tree;
}
else if (PyObject_IsInstance(tree, (PyObject *)&RedBlackTreeType)) {
self->tree = reinterpret_cast<RedBlackTree*>(tree)->sbbt->bst->binary_tree;
}
else if (PyObject_IsInstance(tree, (PyObject *)&SelfBalancingBinaryTreeType)) {
Expand Down
Loading
Loading