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

Adding support for stack.ArrayStack in C++ backend #512

Merged
merged 9 commits into from
Aug 10, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "DynamicArray.hpp"
#include "OneDimensionalArray.hpp"
#include "../../../../utils/_backend/cpp/utils.hpp"
#include <iostream>
czgdp1807 marked this conversation as resolved.
Show resolved Hide resolved

typedef struct {
PyObject_HEAD
Expand Down
3 changes: 2 additions & 1 deletion pydatastructs/miscellaneous_data_structures/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
binomial_trees,
queue,
disjoint_set,
sparse_table
sparse_table,
_extensions,
)

from .binomial_trees import (
Expand Down
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
#ifndef MISCELLANEOUS_DATA_STRUCTURES_ARRAYSTACK_HPP
#define MISCELLANEOUS_DATA_STRUCTURES_ARRAYSTACK_HPP

#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include <cstdlib>
#include <iostream>
#include <structmember.h>
#include "../../../../linear_data_structures/_backend/cpp/arrays/DynamicOneDimensionalArray.hpp"

typedef struct {
PyObject_HEAD
DynamicOneDimensionalArray* _items;
} ArrayStack;

static void ArrayStack_dealloc(ArrayStack *self) {
DynamicOneDimensionalArray_dealloc(self->_items);
Py_TYPE(self)->tp_free(reinterpret_cast<PyObject*>(self));
}

static PyObject* ArrayStack__new__(PyTypeObject *type, PyObject *args, PyObject *kwds) {
// args can be just the data type or the data type and a list of initial values

ArrayStack *self = reinterpret_cast<ArrayStack*>(type->tp_alloc(type, 0));
size_t len_args = PyObject_Length(args);

if (len_args != 1 && len_args != 2) {
PyErr_SetString(PyExc_ValueError,
"Too few arguments to create the stack,"
" pass either only the dtype, or"
" the dtype and a list of initial values");
return NULL;
}

PyObject* items = NULL;
PyObject* doda_kwds = Py_BuildValue("{}");
if (len_args == 1) {
// If the only argument is the dtype, redefine the args as a tuple (dtype, 0)
// where 0 is the initial array size
PyObject* dtype = PyObject_GetItem(args, PyZero);
PyObject* extended_args = PyTuple_Pack(2, dtype, PyLong_FromLong(0));

items = DynamicOneDimensionalArray___new__(&DynamicOneDimensionalArrayType, args, doda_kwds);
} else {
// If the user provides dtype and initial values list, let the array initializer handle the checks.

PyObject* doda_args = PyTuple_Pack(2, PyObject_GetItem(args, PyOne), PyObject_GetItem(args, PyZero));
items = DynamicOneDimensionalArray___new__(&DynamicOneDimensionalArrayType, doda_args, doda_kwds);
}

if (!items) {
return NULL;
}

DynamicOneDimensionalArray* tmp = self->_items;
self->_items = reinterpret_cast<DynamicOneDimensionalArray*>(items);

return reinterpret_cast<PyObject*>(self);
}

static PyObject* ArrayStack__str__(ArrayStack* self){
return DynamicOneDimensionalArray___str__(self->_items);
}

static PyTypeObject ArrayStackType = {
/* tp_name */ PyVarObject_HEAD_INIT(NULL, 0) "ArrayStack",
/* tp_basicsize */ sizeof(ArrayStack),
/* tp_itemsize */ 0,
/* tp_dealloc */ (destructor) ArrayStack_dealloc,
/* tp_print */ 0,
/* tp_getattr */ 0,
/* tp_setattr */ 0,
/* tp_reserved */ 0,
/* tp_repr */ 0,
/* tp_as_number */ 0,
/* tp_as_sequence */ 0,
/* tp_as_mapping */ 0,
/* tp_hash */ 0,
/* tp_call */ 0,
/* tp_str */ (reprfunc) ArrayStack__str__,
/* tp_getattro */ 0,
/* tp_setattro */ 0,
/* tp_as_buffer */ 0,
/* tp_flags */ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
/* tp_doc */ 0,
/* tp_traverse */ 0,
/* tp_clear */ 0,
/* tp_richcompare */ 0,
/* tp_weaklistoffset */ 0,
/* tp_iter */ 0,
/* tp_iternext */ 0,
/* tp_methods */ 0,
/* tp_members */ 0,
/* tp_getset */ 0,
/* tp_base */ 0,
/* tp_dict */ 0,
/* tp_descr_get */ 0,
/* tp_descr_set */ 0,
/* tp_dictoffset */ 0,
/* tp_init */ 0,
/* tp_alloc */ 0,
/* tp_new */ ArrayStack__new__,
};


#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include <Python.h>
#include "ArrayStack.hpp"

static struct PyModuleDef stack_struct = {
PyModuleDef_HEAD_INIT,
"_stack",
0,
-1,
NULL,
};

PyMODINIT_FUNC PyInit__stack(void) {
Py_Initialize();
PyObject *stack = PyModule_Create(&stack_struct);

if (PyType_Ready(&ArrayStackType) < 0) {
return NULL;
}
Py_INCREF(&ArrayStackType);
PyModule_AddObject(stack, "ArrayStack", reinterpret_cast<PyObject*>(&ArrayStackType));

if (PyType_Ready(&ArrayType) < 0) {
return NULL;
}
Py_INCREF(&ArrayType);
PyModule_AddObject(stack, "Array", reinterpret_cast<PyObject*>(&ArrayType));

if (PyType_Ready(&OneDimensionalArrayType) < 0) {
return NULL;
}
Py_INCREF(&OneDimensionalArrayType);
PyModule_AddObject(stack, "OneDimensionalArray", reinterpret_cast<PyObject*>(&OneDimensionalArrayType));

if (PyType_Ready(&DynamicArrayType) < 0) {
return NULL;
}
Py_INCREF(&DynamicArrayType);
PyModule_AddObject(stack, "DynamicArray", reinterpret_cast<PyObject*>(&DynamicArrayType));

if (PyType_Ready(&DynamicOneDimensionalArrayType) < 0) {
return NULL;
}
Py_INCREF(&DynamicOneDimensionalArrayType);
PyModule_AddObject(stack, "DynamicOneDimensionalArray", reinterpret_cast<PyObject*>(&DynamicOneDimensionalArrayType));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The segmentation was being thrown because the array types weren't registered in the stack extension module. So there are two options,

  1. Register them manually (as I did in the above).
  2. Import them at runtime via the APIs in https://docs.python.org/3/c-api/import.html.

For now approach in point 1 seems much easier and efficient because we pre-register everything that's going to be used in the module's implementation. So no need to import them during function calls. The second approach is messier as well (PyImport_Import every now and then whenever we want to use something from another C++ extension module).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The following code works for me for example with 4b028f1 commit.

from pydatastructs.miscellaneous_data_structures._backend.cpp import _stack

array_stack = _stack.ArrayStack([1, 2, 3], int)



return stack;
}
16 changes: 16 additions & 0 deletions pydatastructs/miscellaneous_data_structures/_extensions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from setuptools import Extension

project = 'pydatastructs'

module = 'miscellaneous_data_structures'

backend = '_backend'

cpp = 'cpp'

stack = '.'.join([project, module, backend, cpp, '_stack'])
stack_sources = ['/'.join([project, module, backend, cpp, 'stack', 'stack.cpp'])]

extensions = [
Extension(stack, sources=stack_sources),
]
4 changes: 2 additions & 2 deletions scripts/build/dummy_submodules_data.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
project = 'pydatastructs'

modules = ['linear_data_structures']
modules = ['linear_data_structures', 'miscellaneous_data_structures']

backend = '_backend'

cpp = 'cpp'

dummy_submodules_list = [('_arrays.py', '_algorithms.py')]
dummy_submodules_list = [('_arrays.py', '_algorithms.py'), ('_stack',)]
2 changes: 2 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import setuptools
from pydatastructs import linear_data_structures
from pydatastructs import miscellaneous_data_structures

with open("README.md", "r") as fh:
long_description = fh.read()

extensions = []

extensions.extend(linear_data_structures._extensions.extensions)
extensions.extend(miscellaneous_data_structures._extensions.extensions)

setuptools.setup(
name="cz-pydatastructs",
Expand Down