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

Conversation

nubol23
Copy link
Contributor

@nubol23 nubol23 commented Aug 2, 2022

Adds cpp backend for the stack implementation.

Based on Issue #511

Work in progress

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)

@czgdp1807
Copy link
Member

In addition to #512 (comment) I removed calls to Py_INCREF/Py_XDECREF as they sometimes give segmentation faults. Anyways, avoid the calls to these two unless extremely necessary, like if your code only works after adding them at specific places then feel free to add otherwise ignore.

@czgdp1807
Copy link
Member

Great progress @nubol23. I am glad that you are able to figure things out on your own in Python C-API. Let me know if you get stuck somewhere, we will fix the problem. :-)

@nubol23
Copy link
Contributor Author

nubol23 commented Aug 9, 2022

Thank you very much @czgdp1807 :) I added the remaining changes for the ArrayStack

@codecov
Copy link

codecov bot commented Aug 10, 2022

Codecov Report

Merging #512 (4385edf) into master (3abc026) will increase coverage by 0.005%.
The diff coverage is 100.000%.

@@              Coverage Diff              @@
##            master      #512       +/-   ##
=============================================
+ Coverage   98.523%   98.528%   +0.005%     
=============================================
  Files           31        32        +1     
  Lines         3996      4010       +14     
=============================================
+ Hits          3937      3951       +14     
  Misses          59        59               
Impacted Files Coverage Δ
...astructs/miscellaneous_data_structures/__init__.py 100.000% <ø> (ø)
...ructs/miscellaneous_data_structures/_extensions.py 100.000% <100.000%> (ø)
...datastructs/miscellaneous_data_structures/stack.py 100.000% <100.000%> (ø)

Impacted file tree graph

@czgdp1807 czgdp1807 changed the title Porting stack.py to C++ backend Adding support for stack.ArrayStack in C++ backend Aug 10, 2022
@czgdp1807 czgdp1807 merged commit 8f419fd into codezonediitj:master Aug 10, 2022
@czgdp1807
Copy link
Member

Great job @nubol23. 🎉🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants