Skip to content

Commit 6555bc0

Browse files
committed
Add read-only option support and add TODOs
1 parent 8a0fbdd commit 6555bc0

8 files changed

Lines changed: 41 additions & 29 deletions

File tree

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

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,11 @@ arr = array( array( [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] ) );
7979

8080
The function accepts the following `options`:
8181

82-
- `buffer`: data source. If provided along with a `buffer` argument, the argument takes precedence.
82+
- **buffer**: data source. If provided along with a `buffer` argument, the argument takes precedence.
8383

84-
- `dtype`: underlying storage [data type][@stdlib/ndarray/dtypes]. If not specified and a data source is provided, the data type is inferred from the provided data source. If an input data source is not of the same type, this option specifies the data type to which to cast the input data. For non-[`ndarray`][@stdlib/ndarray/ctor] generic array data sources, the function casts generic array data elements to the default data type. In order to prevent this cast, the `dtype` option **must** be explicitly set to `'generic'`. Any time a cast is required, the `copy` option is set to `true`, as memory must be copied from the data source to an output data buffer. Default: `'float64'`.
84+
- **dtype**: underlying storage [data type][@stdlib/ndarray/dtypes]. If not specified and a data source is provided, the data type is inferred from the provided data source. If an input data source is not of the same type, this option specifies the data type to which to cast the input data. For non-[`ndarray`][@stdlib/ndarray/ctor] generic array data sources, the function casts generic array data elements to the default data type. In order to prevent this cast, the `dtype` option **must** be explicitly set to `'generic'`. Any time a cast is required, the `copy` option is set to `true`, as memory must be copied from the data source to an output data buffer. Default: `'float64'`.
8585

86-
- `order`: specifies the memory layout of the data source as either row-major (C-style) or column-major (Fortran-style). The option may be one of the following values:
86+
- **order**: specifies the memory layout of the data source as either row-major (C-style) or column-major (Fortran-style). The option may be one of the following values:
8787

8888
- `row-major`: the order of the returned array is row-major.
8989
- `column-major`: the order of the returned array is column-major.
@@ -92,15 +92,15 @@ The function accepts the following `options`:
9292

9393
Note that specifying an order which differs from the order of a provided data source does **not** entail a conversion from one memory layout to another. In short, this option is descriptive, not prescriptive. Default: `'row-major'`.
9494

95-
- `shape`: array shape (dimensions). If a shape is not specified, the function attempts to infer a shape based on a provided data source. For example, if provided a nested array, the function resolves nested array dimensions. If provided a multidimensional array data source, the function uses the array's associated shape. For most use cases, such inference suffices. For the remaining use cases, specifying a shape is necessary. For example, provide a shape to create a multidimensional array view over a linear data buffer, ignoring any existing shape meta data associated with a provided data source.
95+
- **shape**: array shape (dimensions). If a shape is not specified, the function attempts to infer a shape based on a provided data source. For example, if provided a nested array, the function resolves nested array dimensions. If provided a multidimensional array data source, the function uses the array's associated shape. For most use cases, such inference suffices. For the remaining use cases, specifying a shape is necessary. For example, provide a shape to create a multidimensional array view over a linear data buffer, ignoring any existing shape meta data associated with a provided data source.
9696

97-
- `flatten`: `boolean` indicating whether to automatically flatten generic array data sources. If an array shape is not specified, the shape is inferred from the dimensions of nested arrays prior to flattening. If a use case requires partial flattening, partially flatten **prior** to invoking this function and set the option value to `false` to prevent further flattening during invocation. Default: `true`.
97+
- **flatten**: `boolean` indicating whether to automatically flatten generic array data sources. If an array shape is not specified, the shape is inferred from the dimensions of nested arrays prior to flattening. If a use case requires partial flattening, partially flatten **prior** to invoking this function and set the option value to `false` to prevent further flattening during invocation. Default: `true`.
9898

99-
- `copy`: `boolean` indicating whether to (shallow) copy source data to a new data buffer. The function does **not** perform a deep copy. To prevent undesired shared changes in state for generic arrays containing objects, perform a deep copy **prior** to invoking this function. Default: `false`.
99+
- **copy**: `boolean` indicating whether to (shallow) copy source data to a new data buffer. The function does **not** perform a deep copy. To prevent undesired shared changes in state for generic arrays containing objects, perform a deep copy **prior** to invoking this function. Default: `false`.
100100

101-
- `ndmin`: specifies the minimum number of dimensions. If an array shape has fewer dimensions than required by `ndmin`, the function **prepends** singleton dimensions to the array shape in order to satisfy the dimensions requirement. Default: `0`.
101+
- **ndmin**: specifies the minimum number of dimensions. If an array shape has fewer dimensions than required by `ndmin`, the function **prepends** singleton dimensions to the array shape in order to satisfy the dimensions requirement. Default: `0`.
102102

103-
- `casting`: specifies the casting rule used to determine acceptable casts. The option may be one of the following values:
103+
- **casting**: specifies the casting rule used to determine acceptable casts. The option may be one of the following values:
104104

105105
- `none`: only allow casting between identical types.
106106
- `equiv`: allow casting between identical and byte swapped types.
@@ -110,15 +110,17 @@ The function accepts the following `options`:
110110

111111
Default: `'safe'`.
112112

113-
- `mode`: specifies how to handle indices which exceed array dimensions.
113+
- **mode**: specifies how to handle indices which exceed array dimensions.
114114

115115
- `throw`: specifies that an [`ndarray`][@stdlib/ndarray/ctor] instance should throw an error when an index exceeds array dimensions.
116116
- `wrap`: specifies that an [`ndarray`][@stdlib/ndarray/ctor] instance should wrap around an index exceeding array dimensions using modulo arithmetic.
117117
- `clamp`: specifies that an [`ndarray`][@stdlib/ndarray/ctor] instance should set an index exceeding array dimensions to either `0` (minimum index) or the maximum index.
118118

119119
Default: `'throw'`.
120120

121-
- `submode`: a mode array which specifies for each dimension how to handle subscripts which exceed array dimensions. If provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. Default: `[ options.mode ]`.
121+
- **submode**: a mode array which specifies for each dimension how to handle subscripts which exceed array dimensions. If provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. Default: `[ options.mode ]`.
122+
123+
- **readonly**: `boolean` indicating whether an [`ndarray`][@stdlib/ndarray/ctor] instance should be **read-only**. Default: `false`.
122124

123125
By default, an [`ndarray`][@stdlib/ndarray/ctor] instance **throws** when provided an index which exceeds array dimensions. To support alternative indexing behavior, set the `mode` option, which will affect all public methods for getting and setting array elements.
124126

lib/node_modules/@stdlib/ndarray/array/docs/repl.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,9 @@
122122

123123
Default: [ options.mode ].
124124

125+
options.readonly: boolean (optional)
126+
Boolean indicating whether an array should be read-only. Default: false.
127+
125128
Returns
126129
-------
127130
out: ndarray

lib/node_modules/@stdlib/ndarray/array/docs/types/index.d.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,9 @@
1919
// TypeScript Version: 2.0
2020

2121
/// <reference types="@stdlib/types"/>
22-
/// <reference types="node"/>
2322

24-
import { Buffer } from 'buffer';
25-
import { ArrayLike, TypedArray } from '@stdlib/types/array';
26-
import { DataType, ndarray, Mode, Order } from '@stdlib/types/ndarray';
23+
import { ArrayLike } from '@stdlib/types/array';
24+
import { DataType, ndarray, Mode, Order, Shape } from '@stdlib/types/ndarray';
2725

2826
/**
2927
* Interface defining function options.
@@ -42,7 +40,7 @@ interface Options {
4240
/**
4341
* Array shape.
4442
*/
45-
shape?: ArrayLike<number>;
43+
shape?: Shape;
4644

4745
/**
4846
* Specifies how to handle indices which exceed array dimensions (default: 'throw').
@@ -73,12 +71,12 @@ interface Options {
7371
* Casting rule used to determine what constitutes an acceptable cast (default: 'safe').
7472
*/
7573
casting?: string;
76-
}
7774

78-
/**
79-
* Array or typed array.
80-
*/
81-
type ArrayOrBufferOrTypedArray = Array<any> | TypedArray | Buffer | null;
75+
/**
76+
* Boolean indicating if an array should be read-only (default: false).
77+
*/
78+
readonly?: boolean;
79+
}
8280

8381
/**
8482
* Returns a multidimensional array.
@@ -94,6 +92,7 @@ type ArrayOrBufferOrTypedArray = Array<any> | TypedArray | Buffer | null;
9492
* @param options.flatten - boolean indicating whether to automatically flatten generic array data sources (default: true)
9593
* @param options.ndmin - minimum number of dimensions (default: 0)
9694
* @param options.casting - casting rule used to determine what constitutes an acceptable cast (default: 'safe')
95+
* @param options.readonly - boolean indicating whether an array should be read-only
9796
* @throws must provide valid options
9897
* @throws must provide either an array shape, data source, or both
9998
* @throws invalid cast
@@ -130,6 +129,7 @@ declare function array( options: Options ): ndarray;
130129
* @param options.flatten - boolean indicating whether to automatically flatten generic array data sources (default: true)
131130
* @param options.ndmin - minimum number of dimensions (default: 0)
132131
* @param options.casting - casting rule used to determine what constitutes an acceptable cast (default: 'safe')
132+
* @param options.readonly - boolean indicating whether an array should be read-only
133133
* @throws must provide valid options
134134
* @throws must provide either an array shape, data source, or both
135135
* @throws invalid cast
@@ -168,7 +168,7 @@ declare function array( options: Options ): ndarray;
168168
* var v = arr.get( 0, 0 );
169169
* // returns 1.0
170170
*/
171-
declare function array( buffer: ArrayOrBufferOrTypedArray, options?: Options ): ndarray; // tslint-disable-line max-line-length
171+
declare function array( buffer: ArrayLike<any>, options?: Options ): ndarray;
172172

173173

174174
// EXPORTS //

lib/node_modules/@stdlib/ndarray/array/docs/types/test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ import array = require( './index' );
3232
array( true ); // $ExpectError
3333
array( false ); // $ExpectError
3434
array( undefined ); // $ExpectError
35-
array( '5' ); // $ExpectError
36-
array( ( x: number ): number => x ); // $ExpectError
35+
array( 5 ); // $ExpectError
36+
array( null ); // $ExpectError
3737
}
3838

3939
// The function does not compile if provided a second argument which is not an options object...

lib/node_modules/@stdlib/ndarray/array/lib/cast_buffer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ function castBuffer( buffer, len, dtype ) {
5858
} else {
5959
out = new ctor( len );
6060
for ( i = 0; i < len; i++ ) {
61-
out[ i ] = buffer[ i ];
61+
out[ i ] = buffer[ i ]; // TODO: wrap and use accessors here and above
6262
}
6363
}
6464
return out;

lib/node_modules/@stdlib/ndarray/array/lib/copy_view.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ function generic( arr ) {
4141
len = arr.length;
4242
out = [];
4343
for ( i = 0; i < len; i++ ) {
44-
out.push( arr.get( i ) );
44+
out.push( arr.get( i ) ); // FIXME: what if `arr` has more than one dimensions?
4545
}
4646
return out;
4747
}
@@ -61,7 +61,7 @@ function binary( arr ) {
6161
len = arr.length;
6262
out = allocUnsafe( len );
6363
for ( i = 0; i < len; i++ ) {
64-
out[ i ] = arr.get( i );
64+
out[ i ] = arr.get( i ); // FIXME: what if `arr` has more than one dimensions?
6565
}
6666
return out;
6767
}
@@ -82,9 +82,9 @@ function typed( arr, dtype ) {
8282

8383
ctor = bufferCtors( dtype );
8484
len = arr.length;
85-
out = new ctor( len );
85+
out = new ctor( len ); // FIXME: need to account for complex number arrays; in which case, we may want to do something similar to `array/convert`
8686
for ( i = 0; i < len; i++ ) {
87-
out[ i ] = arr.get( i );
87+
out[ i ] = arr.get( i ); // FIXME: what if `arr` has more than one dimensions?
8888
}
8989
return out;
9090
}

lib/node_modules/@stdlib/ndarray/array/lib/defaults.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@
55
"flatten": true,
66
"mode": "throw",
77
"ndmin": 0,
8-
"order": "row-major"
8+
"order": "row-major",
9+
"readonly": false
910
}

lib/node_modules/@stdlib/ndarray/array/lib/main.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ var expandStrides = require( './expand_strides.js' );
6464
* @param {boolean} [options.flatten=true] - boolean indicating whether to automatically flatten generic array data sources
6565
* @param {NonNegativeInteger} [options.ndmin=0] - minimum number of dimensions
6666
* @param {string} [options.casting="safe"] - casting rule used to determine what constitutes an acceptable cast
67+
* @param {boolean} [options.readonly=false] - boolean indicating if an array should be read-only
6768
* @throws {TypeError} options argument must be an object
6869
* @throws {TypeError} must provide valid options
6970
* @throws {Error} must provide either an array shape, data source, or both
@@ -245,6 +246,11 @@ function array() {
245246
} else {
246247
nopts.submode = [ nopts.mode ];
247248
}
249+
if ( hasOwnProp( options, 'readonly' ) ) {
250+
nopts.readonly = options.readonly;
251+
} else {
252+
nopts.readonly = defaults.readonly;
253+
}
248254
if ( hasOwnProp( options, 'copy' ) ) {
249255
opts.copy = options.copy;
250256
if ( !isBoolean( opts.copy ) ) {

0 commit comments

Comments
 (0)