Skip to content

Commit 6eebf78

Browse files
committed
Determine if array can be stored contiguously
1 parent 4106514 commit 6eebf78

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

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

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"@stdlib/ndarray/base/bytes-per-element",
3737
"@stdlib/ndarray/base/dtype-char",
3838
"@stdlib/ndarray/base/iteration-order",
39+
"@stdlib/ndarray/base/minmax-view-buffer-index",
3940
"@stdlib/ndarray/base/numel",
4041
"@stdlib/ndarray/base/strides2order",
4142
"@stdlib/ndarray/dtypes",

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

+19
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "stdlib/ndarray/base/bytes_per_element.h"
2222
#include "stdlib/ndarray/base/dtype_char.h"
2323
#include "stdlib/ndarray/base/iteration_order.h"
24+
#include "stdlib/ndarray/base/minmax_view_buffer_index.h"
2425
#include "stdlib/ndarray/base/numel.h"
2526
#include "stdlib/ndarray/base/strides2order.h"
2627
#include "stdlib/ndarray/dtypes.h"
@@ -84,6 +85,8 @@
8485
* free( x );
8586
*/
8687
struct ndarray * stdlib_ndarray_constructor( uint8_t *data, uint64_t ndims, int64_t *shape, int64_t *strides, uint64_t offset, enum STDLIB_NDARRAY_ORDER order, enum STDLIB_NDARRAY_DTYPE dtype ) {
88+
uint8_t contiguous;
89+
uint64_t tmp[2];
8790
uint8_t ord;
8891

8992
struct ndarray *arr = malloc( sizeof( struct ndarray ) );
@@ -108,6 +111,22 @@ struct ndarray * stdlib_ndarray_constructor( uint8_t *data, uint64_t ndims, int6
108111
// Infer the array "order" from the stride array (this is supplementary to the `order` parameter):
109112
ord = stdlib_ndarray_strides2order( ndims, strides );
110113

114+
// Determine if the array can be stored contiguously...
115+
if ( arr->length == 0 || arr->_iterationOrder == 0 ) {
116+
// 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.
117+
contiguous = 0;
118+
} else {
119+
// Ensure that the array is compatible with a single memory segment:
120+
stdlib_ndarray_minmax_view_buffer_index( tmp, ndims, shape, strides, offset );
121+
if ( arr->length == (tmp[1]-tmp[0]+1) ) {
122+
// Compatible:
123+
contiguous = 1;
124+
} else {
125+
// Incompatible:
126+
contiguous = 0;
127+
}
128+
}
129+
111130
// TODO: compute flags
112131

113132
return arr;

0 commit comments

Comments
 (0)