Skip to content

Commit 4c6c935

Browse files
authored
C++ backend for Splay Trees (#562)
1 parent 2d06ac8 commit 4c6c935

File tree

8 files changed

+423
-36
lines changed

8 files changed

+423
-36
lines changed

pydatastructs/trees/_backend/cpp/BinaryIndexedTree.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,11 @@ static PyObject* BinaryIndexedTree___new__(PyTypeObject* type, PyObject *args, P
6666
}
6767
self->array = reinterpret_cast<OneDimensionalArray*>(_one_dimensional_array);
6868
self->tree = PyList_New(self->array->_size+2);
69-
for(int i=0;i<self->array->_size+2;i++){
69+
for(int i=0;i<self->array->_size+2;i++) {
7070
PyList_SetItem(self->tree, i, PyZero);
7171
}
7272
self->flag = PyList_New(self->array->_size);
73-
for(int i=0;i<self->array->_size;i++){
73+
for(int i=0;i<self->array->_size;i++) {
7474
PyList_SetItem(self->flag, i, PyZero);
7575
BinaryIndexedTree_update(self, Py_BuildValue("(OO)", PyLong_FromLong(i), self->array->_data[i]));
7676
}

pydatastructs/trees/_backend/cpp/BinarySearchTree.hpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ static PyObject* BinarySearchTree_search(BinarySearchTree* self, PyObject* args,
103103
PyErr_SetString(PyExc_ValueError, "comparator should be callable");
104104
return NULL;
105105
}
106-
PyObject* arguments = Py_BuildValue("OO", key, curr_key);
106+
PyObject* arguments = Py_BuildValue("(OO)", key, curr_key);
107107
PyObject* res = PyObject_CallObject(bt->comparator, arguments);
108108
Py_DECREF(arguments);
109109
if (!PyLong_Check(res)) {
@@ -125,7 +125,7 @@ static PyObject* BinarySearchTree_search(BinarySearchTree* self, PyObject* args,
125125
return walk;
126126
}
127127
else {
128-
return Py_BuildValue("OO",walk,parent);
128+
return Py_BuildValue("(OO)",walk,parent);
129129
}
130130
Py_RETURN_NONE; // dummy return statement, never executed
131131
}
@@ -168,7 +168,7 @@ static PyObject* BinarySearchTree_insert(BinarySearchTree* self, PyObject* args)
168168
PyErr_SetString(PyExc_ValueError, "comparator should be callable");
169169
return NULL;
170170
}
171-
PyObject* arguments = Py_BuildValue("OO", key, curr_key);
171+
PyObject* arguments = Py_BuildValue("(OO)", key, curr_key);
172172
PyObject* cres = PyObject_CallObject(bt->comparator, arguments);
173173
Py_DECREF(arguments);
174174
if (!PyLong_Check(cres)) {
@@ -359,7 +359,7 @@ static PyObject* BinarySearchTree__bound_helper(BinarySearchTree* self, PyObject
359359
PyErr_SetString(PyExc_ValueError, "comparator should be callable");
360360
return NULL;
361361
}
362-
PyObject* arguments = Py_BuildValue("OO", reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(node_idx)])->key, bound_key);
362+
PyObject* arguments = Py_BuildValue("(OO)", reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(node_idx)])->key, bound_key);
363363
PyObject* cres = PyObject_CallObject(bt->comparator, arguments);
364364
Py_DECREF(arguments);
365365
if (!PyLong_Check(cres)) {
@@ -481,7 +481,7 @@ static PyObject* BinarySearchTree__lca_2(BinarySearchTree* self, PyObject* args)
481481
PyErr_SetString(PyExc_ValueError, "comparator should be callable");
482482
return NULL;
483483
}
484-
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);
484+
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);
485485
PyObject* cres1 = PyObject_CallObject(bt->comparator, arguments1);
486486
Py_DECREF(arguments1);
487487
if (!PyLong_Check(cres1)) {
@@ -494,7 +494,7 @@ static PyObject* BinarySearchTree__lca_2(BinarySearchTree* self, PyObject* args)
494494
PyErr_SetString(PyExc_ValueError, "comparator should be callable");
495495
return NULL;
496496
}
497-
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);
497+
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);
498498
PyObject* cres2 = PyObject_CallObject(bt->comparator, arguments2);
499499
Py_DECREF(arguments2);
500500
if (!PyLong_Check(cres2)) {
@@ -522,7 +522,7 @@ static PyObject* BinarySearchTree__lca_2(BinarySearchTree* self, PyObject* args)
522522
PyErr_SetString(PyExc_ValueError, "comparator should be callable");
523523
return NULL;
524524
}
525-
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);
525+
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);
526526
PyObject* cres1 = PyObject_CallObject(bt->comparator, arguments1);
527527
Py_DECREF(arguments1);
528528
if (!PyLong_Check(cres1)) {
@@ -535,7 +535,7 @@ static PyObject* BinarySearchTree__lca_2(BinarySearchTree* self, PyObject* args)
535535
PyErr_SetString(PyExc_ValueError, "comparator should be callable");
536536
return NULL;
537537
}
538-
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);
538+
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);
539539
PyObject* cres2 = PyObject_CallObject(bt->comparator, arguments2);
540540
Py_DECREF(arguments2);
541541
if (!PyLong_Check(cres2)) {
@@ -616,7 +616,7 @@ static PyObject* BinarySearchTree_select(BinarySearchTree* self, PyObject* args)
616616
PyErr_SetString(PyExc_ValueError, "comparator should be callable");
617617
return NULL;
618618
}
619-
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);
619+
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);
620620
PyObject* cres = PyObject_CallObject(bt->comparator, arguments);
621621
Py_DECREF(arguments);
622622
if (!PyLong_Check(cres)) {
@@ -637,7 +637,7 @@ static PyObject* BinarySearchTree_select(BinarySearchTree* self, PyObject* args)
637637
PyErr_SetString(PyExc_ValueError, "comparator should be callable");
638638
return NULL;
639639
}
640-
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);
640+
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);
641641
PyObject* cres = PyObject_CallObject(bt->comparator, arguments);
642642
Py_DECREF(arguments);
643643
if (!PyLong_Check(cres)) {

pydatastructs/trees/_backend/cpp/BinaryTree.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,10 @@ static struct PyMethodDef BinaryTree_PyMethodDef[] = {
122122
};
123123

124124
static PyMemberDef BinaryTree_PyMemberDef[] = {
125-
{"root_idx", T_OBJECT, offsetof(BinaryTree, root_idx), READONLY, "Index of the root node"},
125+
{"root_idx", T_OBJECT_EX, offsetof(BinaryTree, root_idx), 0, "Index of the root node"},
126126
{"comparator", T_OBJECT, offsetof(BinaryTree, comparator), 0, "Comparator function"},
127127
{"tree", T_OBJECT_EX, offsetof(BinaryTree, tree), 0, "Tree"},
128-
{"size", T_LONG, offsetof(BinaryTree, size), READONLY, "Size of the tree"},
128+
{"size", T_LONG, offsetof(BinaryTree, size), 0, "Size of the tree"},
129129
{"is_order_statistic", T_LONG, offsetof(BinaryTree, is_order_statistic), 0, "Whether the tree is ordered statically or not"},
130130
{NULL} /* Sentinel */
131131
};

pydatastructs/trees/_backend/cpp/BinaryTreeTraversal.hpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "BinarySearchTree.hpp"
1616
#include "SelfBalancingBinaryTree.hpp"
1717
#include "RedBlackTree.hpp"
18+
#include "SplayTree.hpp"
1819

1920
typedef struct {
2021
PyObject_HEAD
@@ -40,8 +41,14 @@ static PyObject* BinaryTreeTraversal___new__(PyTypeObject* type, PyObject *args,
4041
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.
4142
return NULL;
4243
}
44+
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.
45+
return NULL;
46+
}
4347

44-
if (PyObject_IsInstance(tree, (PyObject *)&RedBlackTreeType)) {
48+
if (PyObject_IsInstance(tree, (PyObject *)&SplayTreeType)) {
49+
self->tree = reinterpret_cast<SplayTree*>(tree)->sbbt->bst->binary_tree;
50+
}
51+
else if (PyObject_IsInstance(tree, (PyObject *)&RedBlackTreeType)) {
4552
self->tree = reinterpret_cast<RedBlackTree*>(tree)->sbbt->bst->binary_tree;
4653
}
4754
else if (PyObject_IsInstance(tree, (PyObject *)&SelfBalancingBinaryTreeType)) {

0 commit comments

Comments
 (0)