Skip to content

Commit 832e17f

Browse files
committed
Move logic for determining ndarray flags to separate function
1 parent cf4429c commit 832e17f

File tree

1 file changed

+54
-31
lines changed
  • lib/node_modules/@stdlib/ndarray/ctor/src

1 file changed

+54
-31
lines changed

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

+54-31
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,59 @@ int64_t stdlib_ndarray_bytelength( const struct ndarray *arr ) {
117117
return arr->byteLength;
118118
}
119119

120+
/**
121+
* Returns ndarray flags.
122+
*
123+
* @param arr input ndarray
124+
* @return flags
125+
*/
126+
int64_t stdlib_ndarray_flags( const struct ndarray *arr ) {
127+
uint8_t contiguous;
128+
int64_t *strides;
129+
int64_t tmp[2];
130+
int64_t ndims;
131+
int64_t flags;
132+
int64_t len;
133+
uint8_t ord;
134+
135+
// Cache various ndarray data:
136+
len = arr->length;
137+
ndims = arr->ndims;
138+
strides = arr->strides;
139+
140+
// Initialize the memory for `flags`:
141+
flags = 0;
142+
143+
// Determine if the array can be stored contiguously...
144+
if ( len == 0 || stdlib_ndarray_iteration_order( ndims, strides ) == 0 ) {
145+
// If an array does not contain any elements, then no data to store, and, if the array is unordered, adjacent array elements are not guaranteed to be stored next to each other.
146+
contiguous = 0;
147+
} else {
148+
// Ensure that the array is compatible with a single memory segment:
149+
stdlib_ndarray_minmax_view_buffer_index( tmp, ndims, arr->shape, strides, arr->offset );
150+
if ( len == (tmp[1]-tmp[0]+1) ) {
151+
// Compatible:
152+
contiguous = 1;
153+
} else {
154+
// Incompatible:
155+
contiguous = 0;
156+
}
157+
}
158+
// Determine if the array is row-major/column-major contiguous:
159+
if ( contiguous == 1 ) {
160+
// Infer the array "order" from the stride array (this is supplementary to `arr->order`):
161+
ord = stdlib_ndarray_strides2order( ndims, strides );
162+
163+
if ( ord == 1 || ord == 3 ) {
164+
flags |= STDLIB_NDARRAY_ROW_MAJOR_CONTIGUOUS_FLAG;
165+
}
166+
if ( ord == 2 || ord == 3 ) {
167+
flags |= STDLIB_NDARRAY_COLUMN_MAJOR_CONTIGUOUS_FLAG;
168+
}
169+
}
170+
return flags;
171+
}
172+
120173
/**
121174
* Returns a pointer to a dynamically allocated ndarray.
122175
*
@@ -174,10 +227,7 @@ int64_t stdlib_ndarray_bytelength( const struct ndarray *arr ) {
174227
* free( x );
175228
*/
176229
struct ndarray * stdlib_ndarray_constructor( uint8_t *data, int64_t ndims, int64_t *shape, int64_t *strides, int64_t offset, enum STDLIB_NDARRAY_ORDER order, enum STDLIB_NDARRAY_DTYPE dtype ) {
177-
uint8_t contiguous;
178-
int64_t tmp[2];
179230
int64_t len;
180-
uint8_t ord;
181231

182232
struct ndarray *arr = malloc( sizeof( struct ndarray ) );
183233
if ( arr == NULL ) {
@@ -199,34 +249,7 @@ struct ndarray * stdlib_ndarray_constructor( uint8_t *data, int64_t ndims, int64
199249
arr->byteLength = len * (arr->BYTES_PER_ELEMENT);
200250

201251
arr->_iterationOrder = stdlib_ndarray_iteration_order( ndims, strides );
202-
203-
// Infer the array "order" from the stride array (this is supplementary to the `order` parameter):
204-
ord = stdlib_ndarray_strides2order( ndims, strides );
205-
206-
// Determine if the array can be stored contiguously...
207-
if ( len == 0 || arr->_iterationOrder == 0 ) {
208-
// If an array does not contain any elements, then no data to store, and, if the array is unordered, adjacent array elements are not guaranteed to be stored next to each other.
209-
contiguous = 0;
210-
} else {
211-
// Ensure that the array is compatible with a single memory segment:
212-
stdlib_ndarray_minmax_view_buffer_index( tmp, ndims, shape, strides, offset );
213-
if ( len == (tmp[1]-tmp[0]+1) ) {
214-
// Compatible:
215-
contiguous = 1;
216-
} else {
217-
// Incompatible:
218-
contiguous = 0;
219-
}
220-
}
221-
// Determine if the array is row-major/column-major contiguous:
222-
if ( contiguous == 1 ) {
223-
if ( ord == 1 || ord == 3 ) {
224-
arr->flags |= STDLIB_NDARRAY_ROW_MAJOR_CONTIGUOUS_FLAG;
225-
}
226-
if ( ord == 2 || ord == 3 ) {
227-
arr->flags |= STDLIB_NDARRAY_COLUMN_MAJOR_CONTIGUOUS_FLAG;
228-
}
229-
}
252+
arr->flags = stdlib_ndarray_flags( arr );
230253

231254
return arr;
232255
}

0 commit comments

Comments
 (0)