Skip to content

Commit 2e4fd97

Browse files
committed
Add const qualifier
1 parent 4e91dd8 commit 2e4fd97

File tree

2 files changed

+17
-13
lines changed
  • lib/node_modules/@stdlib/ndarray/ctor

2 files changed

+17
-13
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ uint8_t * stdlib_ndarray_get_ptr( const struct ndarray *arr, const int64_t *sub
266266
/**
267267
* Returns an underlying byte array pointer to an ndarray data element located at a specified linear index.
268268
*/
269-
uint8_t * stdlib_ndarray_iget_ptr( const struct ndarray *arr, int64_t idx );
269+
uint8_t * stdlib_ndarray_iget_ptr( const struct ndarray *arr, const int64_t idx );
270270

271271
/**
272272
* Sets an ndarray data element specified by a provided byte array pointer value.

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

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -336,22 +336,26 @@ uint8_t * stdlib_ndarray_get_ptr( const struct ndarray *arr, const int64_t *sub
336336
* @param idx linear view index
337337
* @return underlying byte array pointer
338338
*/
339-
uint8_t * stdlib_ndarray_iget_ptr( const struct ndarray *arr, int64_t idx ) {
339+
uint8_t * stdlib_ndarray_iget_ptr( const struct ndarray *arr, const int64_t idx ) {
340340
int64_t *strides;
341341
int64_t *shape;
342342
int64_t ndims;
343343
uint8_t *ind;
344344
int64_t s;
345345
int64_t i;
346+
int64_t j;
346347
int8_t io;
347348

349+
// Copy to a mutable variable:
350+
j = idx;
351+
348352
// Resolve an ndarray index based on the ndarray index mode:
349-
idx = stdlib_ndarray_ind( idx, (arr->length)-1, arr->imode );
350-
if ( idx < 0 ) {
353+
j = stdlib_ndarray_ind( j, (arr->length)-1, arr->imode );
354+
if ( j < 0 ) {
351355
return NULL;
352356
}
353357
// Convert the index to units of bytes:
354-
idx *= arr->BYTES_PER_ELEMENT;
358+
j *= arr->BYTES_PER_ELEMENT;
355359

356360
// Determine the pointer to the first indexed element:
357361
ind = (arr->data) + (arr->offset); // pointer arithmetic
@@ -361,30 +365,30 @@ uint8_t * stdlib_ndarray_iget_ptr( const struct ndarray *arr, int64_t idx ) {
361365

362366
// Trivial case where we have all positive strides...
363367
if ( io == 1 ) {
364-
return ind + idx; // pointer arithmetic
368+
return ind + j; // pointer arithmetic
365369
}
366370
// Trivial case where we have all negative strides...
367371
if ( io == -1 ) {
368-
return ind - idx; // pointer arithmetic
372+
return ind - j; // pointer arithmetic
369373
}
370374
// The approach which follows is to resolve a view index to its subscripts and then plug the subscripts into the standard formula for computing the linear index in the underlying byte array...
371375
ndims = arr->ndims;
372376
shape = arr->shape;
373377
strides = arr->strides;
374378
if ( (arr->order) == STDLIB_NDARRAY_COLUMN_MAJOR ) {
375379
for ( i = 0; i < ndims; i++ ) {
376-
s = idx % shape[ i ];
377-
idx -= s;
378-
idx /= shape[ i ];
380+
s = j % shape[ i ];
381+
j -= s;
382+
j /= shape[ i ];
379383
ind += s * strides[ i ]; // pointer arithmetic
380384
}
381385
return ind;
382386
}
383387
// Case: row-major
384388
for ( i = ndims-1; i >= 0; i-- ) {
385-
s = idx % shape[ i ];
386-
idx -= s;
387-
idx /= shape[ i ];
389+
s = j % shape[ i ];
390+
j -= s;
391+
j /= shape[ i ];
388392
ind += s * strides[ i ]; // pointer arithmetic
389393
}
390394
return ind;

0 commit comments

Comments
 (0)