-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
CLN: de-duplicate index validation code #22329
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
Changes from 2 commits
1172bef
7d2e433
92849f4
cc4324c
68e0a67
49ac36b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,23 +44,50 @@ ctypedef fused numeric: | |
cnp.float64_t | ||
|
||
|
||
cdef inline object get_value_at(ndarray arr, object loc): | ||
cdef inline Py_ssize_t validate_indexer(ndarray arr, object loc) except? -1: | ||
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. I don't think you need the question mark in 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. the except is to allow for the IndexError 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. my comment is about the |
||
""" | ||
Cast the given indexer `loc` to an integer. If it is negative, i.e. a | ||
python-style indexing-from-the-end indexer, translate it to a | ||
from-the-front indexer. Raise if this is not possible. | ||
|
||
Parameters | ||
---------- | ||
arr : ndarray | ||
loc : object | ||
|
||
Returns | ||
------- | ||
idx : Py_ssize_t | ||
|
||
Raises | ||
------ | ||
IndexError | ||
""" | ||
cdef: | ||
Py_ssize_t i, sz | ||
Py_ssize_t idx, size | ||
int casted | ||
|
||
if is_float_object(loc): | ||
casted = int(loc) | ||
if casted == loc: | ||
loc = casted | ||
i = <Py_ssize_t> loc | ||
sz = cnp.PyArray_SIZE(arr) | ||
|
||
if i < 0 and sz > 0: | ||
i += sz | ||
elif i >= sz or sz == 0: | ||
idx = <Py_ssize_t>loc | ||
size = cnp.PyArray_SIZE(arr) | ||
|
||
if idx < 0 and size > 0: | ||
idx += size | ||
if idx >= size or size == 0 or idx < 0: | ||
raise IndexError('index out of bounds') | ||
|
||
return idx | ||
|
||
|
||
cdef inline object get_value_at(ndarray arr, object loc): | ||
cdef: | ||
Py_ssize_t i | ||
|
||
i = validate_indexer(arr, loc) | ||
return get_value_1d(arr, i) | ||
|
||
|
||
|
@@ -71,19 +98,9 @@ cdef inline set_value_at_unsafe(ndarray arr, object loc, object value): | |
flag above the loop and then eschew the check on each iteration. | ||
""" | ||
cdef: | ||
Py_ssize_t i, sz | ||
if is_float_object(loc): | ||
casted = int(loc) | ||
if casted == loc: | ||
loc = casted | ||
i = <Py_ssize_t> loc | ||
sz = cnp.PyArray_SIZE(arr) | ||
|
||
if i < 0: | ||
i += sz | ||
elif i >= sz: | ||
raise IndexError('index out of bounds') | ||
Py_ssize_t i | ||
|
||
i = validate_indexer(arr, loc) | ||
assign_value_1d(arr, i, value) | ||
|
||
|
||
|
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.
Since
get_value_at
(the util version below, which is called by theget_value_at
in this file) now has the same validation, is it then not unnecessary to call the validation here as well?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.
I think you’re right, will update.