Skip to content

Commit e225dd8

Browse files
committed
Move function to separate file and create separate header file
1 parent 876105d commit e225dd8

File tree

7 files changed

+125
-75
lines changed

7 files changed

+125
-75
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "ctor/get.h"
2929
#include "ctor/get_ptr.h"
3030
#include "ctor/iget.h"
31+
#include "ctor/iget_ptr.h"
3132
#include "ctor/iset.h"
3233
#include "ctor/macros.h"
3334
#include "ctor/ndarray.h"

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,6 @@
2222
#include <stdint.h>
2323
#include "ndarray.h"
2424

25-
/**
26-
* Returns an underlying byte array pointer to an ndarray data element located at a specified linear index.
27-
*/
28-
uint8_t * stdlib_ndarray_iget_ptr( const struct ndarray *arr, const int64_t idx );
29-
3025
/**
3126
* Returns an ndarray data element located at a specified linear index.
3227
*/
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2018 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
#ifndef STDLIB_NDARRAY_CTOR_IGET_PTR_H
20+
#define STDLIB_NDARRAY_CTOR_IGET_PTR_H
21+
22+
#include <stdint.h>
23+
#include "ndarray.h"
24+
25+
/**
26+
* Returns an underlying byte array pointer to an ndarray data element located at a specified linear index.
27+
*/
28+
uint8_t * stdlib_ndarray_iget_ptr( const struct ndarray *arr, const int64_t idx );
29+
30+
#endif // !STDLIB_NDARRAY_CTOR_IGET_PTR_H

lib/node_modules/@stdlib/ndarray/ctor/manifest.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"./src/get.c",
2929
"./src/get_ptr.c",
3030
"./src/iget.c",
31+
"./src/iget_ptr.c",
3132
"./src/iset.c",
3233
"./src/main.c",
3334
"./src/set.c",

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

Lines changed: 1 addition & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -18,79 +18,11 @@
1818

1919
#include <stdlib.h>
2020
#include <stdint.h>
21-
#include "stdlib/ndarray/base/bytes_per_element.h"
22-
#include "stdlib/ndarray/base/ind.h"
23-
#include "stdlib/ndarray/base/iteration_order.h"
24-
#include "stdlib/ndarray/orders.h"
2521
#include "stdlib/ndarray/ctor/get_ptr.h"
22+
#include "stdlib/ndarray/ctor/iget_ptr.h"
2623
#include "stdlib/ndarray/ctor/ndarray.h"
2724
#include "stdlib/ndarray/ctor/iget.h"
2825

29-
/**
30-
* Returns an underlying byte array pointer to an ndarray data element located at a specified linear index.
31-
*
32-
* @param arr input ndarray
33-
* @param idx linear view index
34-
* @return underlying byte array pointer
35-
*/
36-
uint8_t * stdlib_ndarray_iget_ptr( const struct ndarray *arr, const int64_t idx ) {
37-
int64_t *strides;
38-
int64_t *shape;
39-
int64_t ndims;
40-
uint8_t *ind;
41-
int64_t s;
42-
int64_t i;
43-
int64_t j;
44-
int8_t io;
45-
46-
// Copy to a mutable variable:
47-
j = idx;
48-
49-
// Resolve an ndarray index based on the ndarray index mode:
50-
j = stdlib_ndarray_ind( j, (arr->length)-1, arr->imode );
51-
if ( j < 0 ) {
52-
return NULL;
53-
}
54-
// Convert the index to units of bytes:
55-
j *= arr->BYTES_PER_ELEMENT;
56-
57-
// Determine the pointer to the first indexed element:
58-
ind = (arr->data) + (arr->offset); // pointer arithmetic
59-
60-
// Determine the iteration order based on the ndarray strides:
61-
io = stdlib_ndarray_iteration_order( arr->ndims, arr->strides );
62-
63-
// Trivial case where we have all positive strides...
64-
if ( io == 1 ) {
65-
return ind + j; // pointer arithmetic
66-
}
67-
// Trivial case where we have all negative strides...
68-
if ( io == -1 ) {
69-
return ind - j; // pointer arithmetic
70-
}
71-
// 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...
72-
ndims = arr->ndims;
73-
shape = arr->shape;
74-
strides = arr->strides;
75-
if ( (arr->order) == STDLIB_NDARRAY_COLUMN_MAJOR ) {
76-
for ( i = 0; i < ndims; i++ ) {
77-
s = j % shape[ i ];
78-
j -= s;
79-
j /= shape[ i ];
80-
ind += s * strides[ i ]; // pointer arithmetic
81-
}
82-
return ind;
83-
}
84-
// Case: row-major
85-
for ( i = ndims-1; i >= 0; i-- ) {
86-
s = j % shape[ i ];
87-
j -= s;
88-
j /= shape[ i ];
89-
ind += s * strides[ i ]; // pointer arithmetic
90-
}
91-
return ind;
92-
}
93-
9426
/**
9527
* Returns an ndarray data element located at a specified linear index.
9628
*
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2018 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
#include <stdlib.h>
20+
#include <stdint.h>
21+
#include "stdlib/ndarray/base/bytes_per_element.h"
22+
#include "stdlib/ndarray/base/ind.h"
23+
#include "stdlib/ndarray/base/iteration_order.h"
24+
#include "stdlib/ndarray/orders.h"
25+
#include "stdlib/ndarray/ctor/ndarray.h"
26+
#include "stdlib/ndarray/ctor/iget_ptr.h"
27+
28+
/**
29+
* Returns an underlying byte array pointer to an ndarray data element located at a specified linear index.
30+
*
31+
* @param arr input ndarray
32+
* @param idx linear view index
33+
* @return underlying byte array pointer
34+
*/
35+
uint8_t * stdlib_ndarray_iget_ptr( const struct ndarray *arr, const int64_t idx ) {
36+
int64_t *strides;
37+
int64_t *shape;
38+
int64_t ndims;
39+
uint8_t *ind;
40+
int64_t s;
41+
int64_t i;
42+
int64_t j;
43+
int8_t io;
44+
45+
// Copy to a mutable variable:
46+
j = idx;
47+
48+
// Resolve an ndarray index based on the ndarray index mode:
49+
j = stdlib_ndarray_ind( j, (arr->length)-1, arr->imode );
50+
if ( j < 0 ) {
51+
return NULL;
52+
}
53+
// Convert the index to units of bytes:
54+
j *= arr->BYTES_PER_ELEMENT;
55+
56+
// Determine the pointer to the first indexed element:
57+
ind = (arr->data) + (arr->offset); // pointer arithmetic
58+
59+
// Determine the iteration order based on the ndarray strides:
60+
io = stdlib_ndarray_iteration_order( arr->ndims, arr->strides );
61+
62+
// Trivial case where we have all positive strides...
63+
if ( io == 1 ) {
64+
return ind + j; // pointer arithmetic
65+
}
66+
// Trivial case where we have all negative strides...
67+
if ( io == -1 ) {
68+
return ind - j; // pointer arithmetic
69+
}
70+
// 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...
71+
ndims = arr->ndims;
72+
shape = arr->shape;
73+
strides = arr->strides;
74+
if ( (arr->order) == STDLIB_NDARRAY_COLUMN_MAJOR ) {
75+
for ( i = 0; i < ndims; i++ ) {
76+
s = j % shape[ i ];
77+
j -= s;
78+
j /= shape[ i ];
79+
ind += s * strides[ i ]; // pointer arithmetic
80+
}
81+
return ind;
82+
}
83+
// Case: row-major
84+
for ( i = ndims-1; i >= 0; i-- ) {
85+
s = j % shape[ i ];
86+
j -= s;
87+
j /= shape[ i ];
88+
ind += s * strides[ i ]; // pointer arithmetic
89+
}
90+
return ind;
91+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
#include <stdlib.h>
2020
#include <stdint.h>
21-
#include "stdlib/ndarray/ctor/iget.h"
21+
#include "stdlib/ndarray/ctor/iget_ptr.h"
2222
#include "stdlib/ndarray/ctor/ndarray.h"
2323
#include "stdlib/ndarray/ctor/set_ptr.h"
2424
#include "stdlib/ndarray/ctor/iset.h"

0 commit comments

Comments
 (0)