Skip to content

Commit f22a9ab

Browse files
committed
fix: ensure support for allocating zero-dimensional ndarrays
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: na - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: na - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: passed - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent 7c76342 commit f22a9ab

File tree

2 files changed

+49
-5
lines changed

2 files changed

+49
-5
lines changed

lib/node_modules/@stdlib/ndarray/ctor/README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -880,6 +880,7 @@ struct ndarray * stdlib_ndarray_allocate( int16_t dtype, uint8_t *data, int64_t
880880
Notes:
881881

882882
- The user is responsible for freeing the allocated memory.
883+
- To allocate a zero-dimensional ndarray, provide a `shape` argument equal to `NULL`, an `ndims` argument equal to `0`, and a `strides` argument consisting of a single element equal to `0`. The `order` argument can be either row-major or column-major and has no effect on data storage or access.
883884

884885
#### stdlib_ndarray_bytelength( \*arr )
885886

@@ -999,6 +1000,7 @@ int64_t stdlib_ndarray_dimension( const struct ndarray *arr, const int64_t i );
9991000
Notes:
10001001
10011002
- The function does perform bounds checking for the dimension index.
1003+
- If an input ndarray is zero-dimensional, the function always returns `-1`.
10021004
10031005
#### stdlib_ndarray_disable_flags( \*arr, flags )
10041006
@@ -1505,6 +1507,10 @@ The function accepts the following arguments:
15051507
int64_t * stdlib_ndarray_shape( const struct ndarray *arr );
15061508
```
15071509
1510+
Notes:
1511+
1512+
- If an input ndarray is zero-dimensional, the function returns a null pointer.
1513+
15081514
#### stdlib_ndarray_stride( \*arr, i )
15091515
15101516
Returns an ndarray stride (in bytes).
@@ -1546,7 +1552,7 @@ int64_t stdlib_ndarray_stride( const struct ndarray *arr, const int64_t i );
15461552
15471553
Notes:
15481554
1549-
- the function does perform bounds checking for the dimension index.
1555+
- The function does perform bounds checking for the dimension index.
15501556
15511557
#### stdlib_ndarray_strides( \*arr )
15521558

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

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,29 @@
3737
*
3838
* - The user is responsible for freeing the allocated memory.
3939
*
40+
* - To create a zero-dimensional array,
41+
*
42+
* - provide an `ndims` argument equal to `0`.
43+
* - a `shape` argument equal to `NULL`.
44+
* - a `strides` argument containing a single element equal to `0`.
45+
*
46+
* The `order` argument can be either row-major or column-major and has no effect on data storage or access.
47+
*
48+
* ```javascript
49+
* #include "stdlib/ndarray/ctor.h"
50+
* #include "stdlib/ndarray/dtypes.h"
51+
* #include "stdlib/ndarray/index_modes.h"
52+
* #include "stdlib/ndarray/orders.h"
53+
* #include <stdint.h>
54+
*
55+
* uint8_t buffer[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
56+
* int64_t shape[] = NULL;
57+
* int64_t strides = { 0 };
58+
* int64_t offset = 0;
59+
*
60+
* struct ndarray *x = stdlib_ndarray_allocate( STDLIB_NDARRAY_FLOAT64, buffer, 0, shape, strides, offset, STDLIB_NDARRAY_ROW_MAJOR, STDLIB_NDARRAY_INDEX_ERROR, 1, { STDLIB_NDARRAY_INDEX_ERROR } );
61+
* ```
62+
*
4063
* @param dtype data type
4164
* @param data pointer to the underlying byte array
4265
* @param ndims number of dimensions
@@ -115,7 +138,11 @@ struct ndarray * stdlib_ndarray_allocate( int16_t dtype, uint8_t *data, int64_t
115138
arr->strides = strides;
116139
arr->submodes = submodes;
117140

118-
len = stdlib_ndarray_numel( ndims, shape );
141+
if ( shape == NULL ) {
142+
len = 1;
143+
} else {
144+
len = stdlib_ndarray_numel( ndims, shape );
145+
}
119146
arr->length = len;
120147

121148
arr->BYTES_PER_ELEMENT = stdlib_ndarray_bytes_per_element( dtype );
@@ -146,17 +173,21 @@ uint8_t * stdlib_ndarray_data( const struct ndarray *arr ) {
146173
}
147174

148175
/**
149-
* Returns an ndarray dimension.
176+
* Returns an ndarray dimension size.
150177
*
151178
* ## Notes
152179
*
153180
* - The function does not perform any sanity checks.
181+
* - If an input ndarray is zero-dimensional, the function always returns `-1`.
154182
*
155183
* @param arr input ndarray
156184
* @param i dimension index
157-
* @return dimension
185+
* @return dimension size
158186
*/
159187
int64_t stdlib_ndarray_dimension( const struct ndarray *arr, const int64_t i ) {
188+
if ( arr->shape == NULL ) {
189+
return -1;
190+
}
160191
return arr->shape[ i ];
161192
}
162193

@@ -229,8 +260,11 @@ int64_t stdlib_ndarray_flags( const struct ndarray *arr ) {
229260

230261
// Determine if the array can be stored contiguously...
231262
if ( len == 0 || stdlib_ndarray_iteration_order( ndims, strides ) == 0 ) {
232-
// 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.
263+
// 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:
233264
contiguous = 0;
265+
} else if ( ndims == 0 ) {
266+
// If an array is zero-dimensional, data consists of a single element which means it is trivially compatible with a single memory segment:
267+
contiguous = 1;
234268
} else {
235269
// Ensure that the array is compatible with a single memory segment:
236270
stdlib_ndarray_minmax_view_buffer_index( ndims, arr->shape, strides, arr->offset, tmp );
@@ -343,6 +377,10 @@ int8_t stdlib_ndarray_order( const struct ndarray *arr ) {
343377
/**
344378
* Returns a pointer to an array containing an ndarray shape (dimensions).
345379
*
380+
* ## Notes
381+
*
382+
* - If an input ndarray is zero-dimensional, the function returns a null pointer.
383+
*
346384
* @param arr input ndarray
347385
* @return array shape (dimensions)
348386
*/

0 commit comments

Comments
 (0)