-
Notifications
You must be signed in to change notification settings - Fork 20
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
feat: modify VectorInit to handle numpy multi dimensional arrays #28
Closed
Closed
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
679c22d
chore: modify VectorInit to handle numyp multi dimensional arrays
Khushiyant ae088a2
chore: add ndim-1 -- draft support
Khushiyant 2b52c7e
refactor: type checks and errors messages
Khushiyant f61220b
fix: non-matching function call
Khushiyant 2a09e38
refactor: code logic--loop
Khushiyant c95571a
chore: add 2 approaches as comments
Khushiyant 2e6e14a
chore: implemented approach similar to arrayinit
Khushiyant 0cc4d6c
chore: add iter--handling for nested arrays
Khushiyant 6416c49
chore: remove unused--si_call var
Khushiyant 357c15e
chore: update pyobject method to __array__
Khushiyant c45bef3
chore: add 1d logic
Khushiyant 2fd19a2
chore: reformmating code sample for readabilty
Khushiyant 363b864
chore: add logic for nd arrays using recurssive call
Khushiyant 2d5f981
chore: remove numpy core directories
Khushiyant 0fea37e
chore: sync to latest commit history
Khushiyant 3ac1fdf
feat: 1D numpy array supprt
Khushiyant 873bcd2
refactor: seperate the index value logic
Khushiyant 950fea2
feat: add support for n-d numpy array
Khushiyant b21587b
refactor: seperate out logic for recursion
Khushiyant 8101d93
chore: remove print statements
Khushiyant File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,6 +52,24 @@ bool HasAttrDirect(PyObject* pyclass, PyObject* pyname, bool mustBeCPyCppyy = fa | |
return false; | ||
} | ||
|
||
template <typename T> | ||
T Get_IndexValue(Py_buffer *view, int i) | ||
{ | ||
if (!view || !view->buf) | ||
{ | ||
// Handle the error, e.g., throw an exception or return a default value | ||
PyErr_SetString(PyExc_RuntimeError, "Buffer is not valid"); | ||
} | ||
|
||
if (i < 0 || i >= view->len / view->itemsize) | ||
{ | ||
// Handle the error, e.g., throw an exception or return a default value | ||
PyErr_SetString(PyExc_IndexError, "Index out of range"); | ||
} | ||
// get the value at index i in the view | ||
return *(T *)((char *)view->buf + i * view->strides[0]); | ||
} | ||
|
||
PyObject* GetAttrDirect(PyObject* pyclass, PyObject* pyname) { | ||
// get an attribute without causing getattr lookups | ||
PyObject* dct = PyObject_GetAttr(pyclass, PyStrings::gDict); | ||
|
@@ -459,7 +477,96 @@ PyObject* VectorIAdd(PyObject* self, PyObject* args, PyObject* /* kwds */) | |
PyErr_SetString(PyExc_TypeError, "argument is not iterable"); | ||
return nullptr; // error already set | ||
} | ||
PyObject *recursive_vector_init(PyObject *self, Py_buffer *view, PyObject *result, int ndim) | ||
{ | ||
// PyObject *tmp_result = PyList_New(0); | ||
if (ndim == 1) | ||
{ | ||
if (!result) | ||
return nullptr; | ||
|
||
Py_ssize_t fillsz = view->len / view->itemsize; | ||
PyObject *pb_call = PyObject_GetAttrString(self, "push_back"); | ||
|
||
for (Py_ssize_t i = 0; i < fillsz; i++) | ||
{ | ||
int val = Get_IndexValue<int>(view, i); | ||
PyObject *item = PyLong_FromLong(val); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Mapping of TConverter* conv = CreateConverter(type_returned); |
||
|
||
if (!item) | ||
{ | ||
Py_DECREF(result); | ||
Py_XDECREF(pb_call); | ||
return nullptr; | ||
} | ||
|
||
PyObject *pbres = PyObject_CallFunctionObjArgs(pb_call, item, nullptr); | ||
Py_DECREF(item); | ||
|
||
if (!pbres) | ||
{ | ||
Py_DECREF(result); | ||
Py_XDECREF(pb_call); | ||
return nullptr; | ||
} | ||
|
||
Py_DECREF(pbres); | ||
} | ||
|
||
Py_XDECREF(pb_call); | ||
return result; | ||
} | ||
|
||
if (!result) | ||
return nullptr; | ||
|
||
Py_ssize_t *shape = (Py_ssize_t *)view->shape; | ||
Py_ssize_t *strides = (Py_ssize_t *)view->strides; | ||
Py_ssize_t *subshape = shape + 1; | ||
Py_ssize_t *substrides = strides + 1; | ||
|
||
for (Py_ssize_t i = 0; i < view->ndim; i++) | ||
{ | ||
Py_buffer subview; | ||
subview.buf = (void *)((char *)view->buf + i * strides[0]); | ||
subview.obj = NULL; | ||
subview.len = subshape[0] * substrides[0]; | ||
subview.readonly = view->readonly; | ||
subview.itemsize = view->itemsize; | ||
subview.format = view->format; | ||
subview.ndim = ndim - 1; | ||
subview.shape = subshape; | ||
subview.strides = substrides; | ||
subview.suboffsets = view->suboffsets; | ||
subview.internal = view->internal; | ||
|
||
PyObject *subresult = recursive_vector_init(self, &subview, result, ndim - 1); | ||
if (!subresult) | ||
{ | ||
Py_DECREF(result); | ||
return nullptr; | ||
} | ||
PyObject *pb_call = PyObject_GetAttrString(self, "push_back"); | ||
|
||
|
||
PyObject *pbres = PyObject_CallFunctionObjArgs(pb_call, subresult, nullptr); | ||
|
||
if (!pbres) | ||
{ | ||
Py_DECREF(result); | ||
Py_XDECREF(pb_call); | ||
return nullptr; | ||
} | ||
|
||
|
||
Py_DECREF(pbres); | ||
Py_DECREF(pb_call); | ||
|
||
Py_DECREF(subresult); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
PyObject* VectorInit(PyObject* self, PyObject* args, PyObject* /* kwds */) | ||
{ | ||
|
@@ -490,10 +597,41 @@ PyObject* VectorInit(PyObject* self, PyObject* args, PyObject* /* kwds */) | |
return result; | ||
} | ||
|
||
// The given argument wasn't iterable: simply forward to regular constructor | ||
PyObject* realInit = PyObject_GetAttr(self, PyStrings::gRealInit); | ||
if (realInit) { | ||
PyObject* result = PyObject_Call(realInit, args, nullptr); | ||
// get the first argument | ||
PyObject* fi = PyTuple_GET_ITEM(args, 0); | ||
if (fi == Py_None) | ||
{ | ||
// empty vector | ||
return PyObject_CallMethodNoArgs(self, PyStrings::gRealInit); | ||
} | ||
|
||
// check if numpy is passed | ||
if (PyObject_CheckBuffer(fi)){ | ||
// create a memoryview | ||
PyObject* memoryview = PyMemoryView_FromObject(fi); | ||
Py_buffer* view = PyMemoryView_GET_BUFFER(memoryview); | ||
|
||
// check if memoryview is valid | ||
if (view->buf == NULL) return nullptr; | ||
|
||
PyObject *result = PyObject_CallMethodNoArgs(self, PyStrings::gRealInit); | ||
|
||
result = recursive_vector_init(self, view, result, view->ndim); | ||
|
||
// dereference the memoryview buffer | ||
PyBuffer_Release(view); | ||
Py_DECREF(memoryview); | ||
Py_DECREF(fi); | ||
|
||
return result; | ||
|
||
} | ||
|
||
// The given argument wasn't iterable or a numpy array: simply forward to regular constructor | ||
PyObject *realInit = PyObject_GetAttr(self, PyStrings::gRealInit); | ||
if (realInit) | ||
{ | ||
PyObject *result = PyObject_Call(realInit, args, nullptr); | ||
Py_DECREF(realInit); | ||
return result; | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.