Skip to content

Commit a43f634

Browse files
committed
Add a "generic" API for retrieving an ndarray data element via a linear index
1 parent 5a1e80f commit a43f634

File tree

2 files changed

+26
-0
lines changed
  • lib/node_modules/@stdlib/ndarray/ctor

2 files changed

+26
-0
lines changed

lib/node_modules/@stdlib/ndarray/ctor/include/stdlib/ndarray/ctor.h

+5
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,11 @@ int8_t stdlib_ndarray_get_int8( const struct ndarray *arr, const int64_t *sub, i
328328
*/
329329
uint8_t * stdlib_ndarray_iget_ptr( const struct ndarray *arr, const int64_t idx );
330330

331+
/**
332+
* Returns an ndarray data element located at a specified linear index.
333+
*/
334+
int8_t stdlib_ndarray_iget( const struct ndarray *arr, const int64_t idx, void *out );
335+
331336
/**
332337
* Returns a double-precision floating-point ndarray data element located at a specified linear index.
333338
*/

lib/node_modules/@stdlib/ndarray/ctor/src/main.c

+21
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,27 @@ uint8_t * stdlib_ndarray_iget_ptr( const struct ndarray *arr, const int64_t idx
674674
return ind;
675675
}
676676

677+
/**
678+
* Returns an ndarray data element located at a specified linear index.
679+
*
680+
* ## Notes
681+
*
682+
* - The function returns `-1` if unable to get an element and `0` otherwise.
683+
* - The function requires a `void` pointer for the output address `out` in order to provide a generic API supporting ndarrays having different data types.
684+
*
685+
* @param arr input ndarray
686+
* @param idx linear view index
687+
* @param out output address
688+
* @return status code
689+
*/
690+
int8_t stdlib_ndarray_iget( const struct ndarray *arr, const int64_t idx, void *out ) {
691+
uint8_t *ind = stdlib_ndarray_iget_ptr( arr, idx );
692+
if ( ind == NULL ) {
693+
return -1;
694+
}
695+
return stdlib_ndarray_get_ptr_value( arr, ind, out );
696+
}
697+
677698
/**
678699
* Returns a double-precision floating-point ndarray data element located at a specified linear index.
679700
*

0 commit comments

Comments
 (0)