@stdlib/array-struct-factory 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +177 -0
- package/NOTICE +1 -0
- package/README.md +413 -0
- package/SECURITY.md +5 -0
- package/dist/index.js +19 -0
- package/dist/index.js.map +7 -0
- package/lib/from_array.js +101 -0
- package/lib/from_iterator.js +48 -0
- package/lib/index.js +107 -0
- package/lib/main.js +573 -0
- package/package.json +89 -0
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../lib/from_array.js", "../lib/from_iterator.js", "../lib/main.js", "../lib/index.js"],
|
|
4
|
+
"sourcesContent": ["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2025 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar getter = require( '@stdlib/array-base-getter' );\nvar accessorGetter = require( '@stdlib/array-base-accessor-getter' );\nvar Uint8Array = require( '@stdlib/array-uint8' );\nvar gcopy = require( '@stdlib/blas-base-gcopy' ).ndarray;\n\n\n// MAIN //\n\n/**\n* Fills an output ArrayBuffer with array values.\n*\n* @private\n* @param {Function} Struct - struct constructor\n* @param {ArrayBuffer} buf - output data buffer\n* @param {Collection} arr - input array\n* @throws {TypeError} an input array must contain struct instances\n* @throws {TypeError} each element of an input array must be a struct instance having the expected layout\n* @returns {ArrayBuffer} output data buffer\n*/\nfunction fromArray( Struct, buf, arr ) {\n\tvar sbytes;\n\tvar bbytes;\n\tvar offset;\n\tvar layout;\n\tvar sview;\n\tvar opts;\n\tvar len;\n\tvar get;\n\tvar flg;\n\tvar nb;\n\tvar v;\n\tvar i;\n\n\topts = {\n\t\t'format': 'layout'\n\t};\n\n\tlen = arr.length;\n\tif ( arr.get && arr.set ) {\n\t\tget = accessorGetter( 'default' );\n\t} else {\n\t\tget = getter( 'default' );\n\t}\n\tlayout = Struct.layout;\n\tnb = Struct.byteLength;\n\n\t// FIXME: add optimization for when `buf` is a StructArray having the same layout, as can just copy bytes\n\n\tbbytes = new Uint8Array( buf );\n\toffset = 0;\n\tfor ( i = 0; i < len; i++ ) {\n\t\tv = get( arr, i );\n\t\ttry {\n\t\t\tsview = Struct.viewOf( v ); // note: this should throw if `v` is not a struct\n\t\t\tflg = true;\n\t\t} catch ( err ) { // eslint-disable-line no-unused-vars\n\t\t\ttry {\n\t\t\t\t// Attempt to convert the input value to a struct instance:\n\t\t\t\tv = new Struct( v ); // note: this should throw if `v` is not an object with valid fields\n\t\t\t} catch ( err ) { // eslint-disable-line no-unused-vars\n\t\t\t\treturn null;\n\t\t\t}\n\t\t\tsview = Struct.viewOf( v );\n\t\t\tflg = false;\n\t\t}\n\t\tif ( flg && v.toString( opts ) !== layout ) {\n\t\t\treturn null;\n\t\t}\n\t\tsbytes = new Uint8Array( sview.buffer, sview.byteOffset, sview.byteLength ); // eslint-disable-line max-len\n\t\tgcopy( nb, sbytes, 1, 0, bbytes, 1, offset );\n\t\toffset += nb;\n\t}\n\treturn buf;\n}\n\n\n// EXPORTS //\n\nmodule.exports = fromArray;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2025 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MAIN //\n\n/**\n* Returns an array of iterated values.\n*\n* @private\n* @param {Object} it - iterator\n* @returns {Array} output array\n*/\nfunction fromIterator( it ) {\n\tvar out;\n\tvar v;\n\n\tout = [];\n\twhile ( true ) {\n\t\tv = it.next();\n\t\tif ( v.done ) {\n\t\t\tbreak;\n\t\t}\n\t\tout.push( v.value );\n\t}\n\treturn out;\n}\n\n\n// EXPORTS //\n\nmodule.exports = fromIterator;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2025 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n/* eslint-disable no-restricted-syntax, no-invalid-this, max-len, max-lines-per-function */\n\n'use strict';\n\n// MODULES //\n\nvar isStructConstructorLike = require( '@stdlib/assert-is-struct-constructor-like' );\nvar isNonNegativeInteger = require( '@stdlib/assert-is-nonnegative-integer' ).isPrimitive;\nvar isInteger = require( '@stdlib/assert-is-integer' ).isPrimitive;\nvar isArrayBuffer = require( '@stdlib/assert-is-arraybuffer' );\nvar isCollection = require( '@stdlib/assert-is-collection' );\nvar isFunction = require( '@stdlib/assert-is-function' );\nvar isObject = require( '@stdlib/assert-is-object' );\nvar isPrototypeOf = require( '@stdlib/assert-is-prototype-of' ); // eslint-disable-line stdlib/no-redeclare\nvar hasIteratorSymbolSupport = require( '@stdlib/assert-has-iterator-symbol-support' );\nvar hasProp = require( '@stdlib/assert-has-property' );\nvar contains = require( '@stdlib/array-base-assert-contains' );\nvar setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' );\nvar setReadOnlyAccessor = require( '@stdlib/utils-define-nonenumerable-read-only-accessor' );\nvar ITERATOR_SYMBOL = require( '@stdlib/symbol-iterator' );\nvar ArrayBuffer = require( '@stdlib/array-buffer' );\nvar Uint8Array = require( '@stdlib/array-uint8' );\nvar getter = require( '@stdlib/array-base-getter' );\nvar accessorGetter = require( '@stdlib/array-base-accessor-getter' );\nvar gcopy = require( '@stdlib/blas-base-gcopy' ).ndarray;\nvar structFactory = require( '@stdlib/dstructs-struct' );\nvar format = require( '@stdlib/string-format' );\nvar fromArray = require( './from_array.js' );\nvar fromIterator = require( './from_iterator.js' );\n\n\n// VARIABLES //\n\nvar HAS_ITERATOR_SYMBOL = hasIteratorSymbolSupport();\nvar CTOR_NAME = 'StructArray';\n\n\n// MAIN //\n\n/**\n* Returns a constructor for creating arrays having a fixed-width composite data type.\n*\n* @param {(Function|Array<Object>)} arg - struct constructor or struct schema\n* @throws {TypeError} first argument must be either a struct constructor or struct schema\n* @returns {Function} constructor\n*\n* @example\n* var Complex128 = require( '@stdlib/complex-float64-ctor' );\n* var structFactory = require( '@stdlib/dstructs-struct' );\n*\n* var schema1 = [\n* {\n* 'name': 're',\n* 'type': 'float64'\n* },\n* {\n* 'name': 'im',\n* 'type': 'float64'\n* }\n* ];\n*\n* // Create a struct constructor for storing real and imaginary components:\n* var Components = structFactory( schema1 );\n*\n* var schema2 = [\n* {\n* 'type': 'union',\n* 'fields': [\n* {\n* 'name': 'value',\n* 'type': 'complex128'\n* },\n* {\n* 'name': 'components',\n* 'type': Components\n* }\n* ]\n* }\n* ];\n*\n* // Create a struct constructor for storing a double-precision complex number:\n* var Complex128Struct = structFactory( schema2 );\n*\n* // Create an array constructor for storing complex numbers:\n* var Complex128Array = factory( Complex128Struct );\n*\n* // Create a new array:\n* var x = new Complex128Array( 10 );\n* // returns <StructArray>\n*\n* // Retrieve the first element:\n* var v1 = x.get( 0 );\n*\n* // Resolve the complex number stored within the first element:\n* var z1 = v1.value;\n* // returns <Complex128>[ 0.0, 0.0 ]\n*\n* // Resolve the individual real and imaginary components:\n* var z2 = v1.components;\n*\n* var re = z2.re;\n* // returns 0.0\n*\n* var im = z2.im;\n* // returns 0.0\n*\n* // Create a new complex number struct:\n* var z3 = new Complex128Struct({\n* 'value': new Complex128( 3.0, 5.0 )\n* });\n*\n* // Update the first element of the array:\n* x.set( z3, 0 );\n*\n* // As `v1` is a view on same memory as the first element, resolve the complex number stored within the element:\n* var z4 = v1.value;\n* // returns <Complex128>[ 3.0, 5.0 ]\n*/\nfunction factory( arg ) { // eslint-disable-line stdlib/jsdoc-require-throws-tags\n\tvar BYTES_PER_ELEMENT;\n\tvar LAYOUT;\n\tvar FIELDS;\n\tvar Struct;\n\n\t// FIXME: add option support for strict input object validation (e.g., throw whenever non-struct properties are included on an object passed to `set`)\n\n\tif ( isCollection( arg ) ) {\n\t\tStruct = structFactory( arg ); // NOTE: delegate to `structFactory` to perform input validation\n\t} else if ( isStructConstructorLike( arg ) ) {\n\t\tStruct = arg;\n\t} else {\n\t\tthrow new TypeError( format( 'invalid argument. First argument must be either a struct constructor or struct schema. Value: `%s`.', arg ) );\n\t}\n\tBYTES_PER_ELEMENT = Struct.byteLength;\n\tLAYOUT = Struct.layout; // TODO: consider whether to lazily materialize the struct layout, as this could potentially be a long string (hence increased memory consumption) depending on the complexity of the struct\n\tFIELDS = Struct.fields;\n\n\t/**\n\t* Constructor which returns an array having a fixed-width composite data type.\n\t*\n\t* @private\n\t* @constructor\n\t* @param {(NonNegativeInteger|Collection|ArrayBuffer|Iterable)} [arg] - length, typed array, array-like object, buffer, or an iterable\n\t* @param {NonNegativeInteger} [byteOffset=0] - byte offset\n\t* @param {NonNegativeInteger} [length] - view length\n\t* @throws {TypeError} must provide a valid first argument\n\t* @throws {TypeError} second argument must be a nonnegative integer\n\t* @throws {TypeError} third argument must be a nonnegative integer\n\t* @throws {RangeError} must provide sufficient memory to accommodate byte offset and view length requirements\n\t* @throws {RangeError} second argument must be a multiple of struct byte length\n\t* @throws {RangeError} second argument must not exceeds the ArrayBuffer bounds\n\t* @throws {TypeError} view length must be a positive multiple of struct byte length\n\t* @throws {TypeError} an input array must contain valid elements\n\t* @returns {StructArray} struct array instance\n\t*/\n\tfunction StructArray( arg, byteOffset, length ) {\n\t\tvar nargs;\n\t\tvar buf;\n\t\tvar len;\n\t\tvar tmp;\n\n\t\tnargs = arguments.length;\n\t\tif ( !( this instanceof StructArray) ) {\n\t\t\tif ( nargs === 0 ) {\n\t\t\t\treturn new StructArray();\n\t\t\t}\n\t\t\tif ( nargs === 1 ) {\n\t\t\t\treturn new StructArray( arg );\n\t\t\t}\n\t\t\tif ( nargs === 2 ) {\n\t\t\t\treturn new StructArray( arg, byteOffset );\n\t\t\t}\n\t\t\treturn new StructArray( arg, byteOffset, length );\n\t\t}\n\n\t\t// Case: new StructArray()\n\t\tif ( nargs === 0 ) {\n\t\t\tbuf = new ArrayBuffer( 0 );\n\t\t\tlen = 0;\n\t\t}\n\t\t// Case: new StructArray( arg )\n\t\telse if ( nargs === 1 ) {\n\t\t\t// Case: new StructArray( length )\n\t\t\tif ( isNonNegativeInteger( arg ) ) {\n\t\t\t\tbuf = new ArrayBuffer( arg*BYTES_PER_ELEMENT );\n\t\t\t\tlen = arg;\n\t\t\t}\n\t\t\t// Case: new StructArray( collection )\n\t\t\telse if ( isCollection( arg ) ) {\n\t\t\t\tlen = arg.length;\n\t\t\t\tbuf = fromArray( Struct, new ArrayBuffer( len*BYTES_PER_ELEMENT ), arg );\n\t\t\t\tif ( buf === null ) {\n\t\t\t\t\tthrow new TypeError( format( 'invalid argument. Each element of a provided input array must be a valid object or a struct instance having the same layout as elements in the desired output array.' ) );\n\t\t\t\t}\n\t\t\t}\n\t\t\t// Case: new StructArray( ArrayBuffer )\n\t\t\telse if ( isArrayBuffer( arg ) ) {\n\t\t\t\tbuf = arg;\n\t\t\t\tlen = buf.byteLength / BYTES_PER_ELEMENT;\n\t\t\t\tif ( !isInteger( len ) ) {\n\t\t\t\t\tthrow new RangeError( format( 'invalid argument. ArrayBuffer byte length must be a multiple of %u. Byte length: `%u`.', BYTES_PER_ELEMENT, buf.byteLength ) );\n\t\t\t\t}\n\t\t\t}\n\t\t\t// Case: new StructArray( iterable )\n\t\t\telse if ( isObject( arg ) ) {\n\t\t\t\tif ( HAS_ITERATOR_SYMBOL === false ) {\n\t\t\t\t\tthrow new TypeError( format( 'invalid argument. Environment lacks Symbol.iterator support. First argument must be a length, ArrayBuffer, typed array, or array-like object. Value: `%s`.', arg ) );\n\t\t\t\t}\n\t\t\t\tif ( !isFunction( arg[ ITERATOR_SYMBOL ] ) ) {\n\t\t\t\t\tthrow new TypeError( format( 'invalid argument. First argument must be a length, ArrayBuffer, typed array, array-like object, or an iterable. Value: `%s`.', arg ) );\n\t\t\t\t}\n\t\t\t\tbuf = arg[ ITERATOR_SYMBOL ]();\n\t\t\t\tif ( !isFunction( buf.next ) ) {\n\t\t\t\t\tthrow new TypeError( format( 'invalid argument. First argument must be a length, ArrayBuffer, typed array, array-like object, or an iterable. Value: `%s`.', arg ) );\n\t\t\t\t}\n\t\t\t\ttmp = fromIterator( buf );\n\t\t\t\tlen = tmp.length;\n\t\t\t\tbuf = fromArray( Struct, new ArrayBuffer( len*BYTES_PER_ELEMENT ), tmp );\n\t\t\t\tif ( buf === null ) {\n\t\t\t\t\tthrow new TypeError( format( 'invalid argument. Each element of a provided input iterable must be either a valid object or a struct instance having the same layout as elements in the desired output array.' ) );\n\t\t\t\t}\n\t\t\t}\n\t\t\t// Case: new StructArray( ???? )\n\t\t\telse {\n\t\t\t\tthrow new TypeError( format( 'invalid argument. First argument must be a length, ArrayBuffer, typed array, array-like object, or an iterable. Value: `%s`.', arg ) );\n\t\t\t}\n\t\t}\n\t\t// Case: new StructArray( ArrayBuffer, byteOffset[, length] )\n\t\telse {\n\t\t\tbuf = arguments[ 0 ];\n\t\t\tif ( !isArrayBuffer( buf ) ) {\n\t\t\t\tthrow new TypeError( format( 'invalid argument. First argument must be an ArrayBuffer. Value: `%s`.', arg ) );\n\t\t\t}\n\t\t\tif ( !isNonNegativeInteger( byteOffset ) ) {\n\t\t\t\tthrow new TypeError( format( 'invalid argument. Second argument must be a nonnegative integer. Value: `%s`.', byteOffset ) );\n\t\t\t}\n\t\t\tif ( !isInteger( byteOffset/BYTES_PER_ELEMENT ) ) {\n\t\t\t\tthrow new RangeError( format( 'invalid argument. Second argument must be a multiple of %u. Value: `%u`.', BYTES_PER_ELEMENT, byteOffset ) );\n\t\t\t}\n\t\t\tif ( byteOffset >= buf.byteLength ) {\n\t\t\t\tthrow new RangeError( format( 'invalid argument. Second argument exceeds the bounds of the ArrayBuffer. Value: `%s`.', byteOffset ) );\n\t\t\t}\n\t\t\t// Case: new StructArray( ArrayBuffer, byteOffset )\n\t\t\tif ( nargs === 2 ) {\n\t\t\t\tlen = ( buf.byteLength - byteOffset ) / BYTES_PER_ELEMENT;\n\t\t\t\tif ( !isInteger( len ) ) {\n\t\t\t\t\tthrow new RangeError( format( 'invalid argument. ArrayBuffer view byte length must be a multiple of %u. View byte length: `%u`.', BYTES_PER_ELEMENT, buf.byteLength-byteOffset ) );\n\t\t\t\t}\n\t\t\t}\n\t\t\t// Case: new StructArray( ArrayBuffer, byteOffset, length )\n\t\t\telse {\n\t\t\t\tlen = length;\n\t\t\t\tif ( !isNonNegativeInteger( len ) ) {\n\t\t\t\t\tthrow new TypeError( format( 'invalid argument. Third argument must be a nonnegative integer. Value: `%s`.', len ) );\n\t\t\t\t}\n\t\t\t\tif ( (len*BYTES_PER_ELEMENT) > (buf.byteLength-byteOffset) ) {\n\t\t\t\t\tthrow new RangeError( format( 'invalid arguments. ArrayBuffer has insufficient capacity. Either decrease the array length or provide a bigger buffer. Minimum capacity: `%u`.', len*BYTES_PER_ELEMENT ) );\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tsetReadOnly( this, '_buffer', buf );\n\t\tsetReadOnly( this, '_byteOffset', byteOffset || 0 );\n\t\tsetReadOnly( this, '_byteLength', len*BYTES_PER_ELEMENT );\n\t\tsetReadOnly( this, '_length', len );\n\t\treturn this;\n\t}\n\n\t/**\n\t* Size (in bytes) of each array element.\n\t*\n\t* @private\n\t* @name BYTES_PER_ELEMENT\n\t* @memberof StructArray\n\t* @readonly\n\t* @type {PositiveInteger}\n\t*/\n\tsetReadOnly( StructArray, 'BYTES_PER_ELEMENT', BYTES_PER_ELEMENT );\n\n\t/**\n\t* Constructor name.\n\t*\n\t* @private\n\t* @name name\n\t* @memberof StructArray\n\t* @readonly\n\t* @type {string}\n\t*/\n\tsetReadOnly( StructArray, 'name', CTOR_NAME );\n\n\t/**\n\t* Element constructor.\n\t*\n\t* @private\n\t* @name struct\n\t* @memberof StructArray\n\t* @readonly\n\t* @type {Function}\n\t*/\n\tsetReadOnly( StructArray, 'struct', Struct );\n\n\t/**\n\t* Pointer to the underlying data buffer.\n\t*\n\t* @private\n\t* @name buffer\n\t* @memberof StructArray.prototype\n\t* @readonly\n\t* @type {ArrayBuffer}\n\t*/\n\tsetReadOnlyAccessor( StructArray.prototype, 'buffer', function get() {\n\t\treturn this._buffer;\n\t});\n\n\t/**\n\t* Size (in bytes) of the array.\n\t*\n\t* @private\n\t* @name byteLength\n\t* @memberof StructArray.prototype\n\t* @readonly\n\t* @type {NonNegativeInteger}\n\t*/\n\tsetReadOnlyAccessor( StructArray.prototype, 'byteLength', function get() {\n\t\treturn this._byteLength;\n\t});\n\n\t/**\n\t* Offset (in bytes) of the array from the start of its underlying `ArrayBuffer`.\n\t*\n\t* @private\n\t* @name byteOffset\n\t* @memberof StructArray.prototype\n\t* @readonly\n\t* @type {NonNegativeInteger}\n\t*/\n\tsetReadOnlyAccessor( StructArray.prototype, 'byteOffset', function get() {\n\t\treturn this._byteOffset;\n\t});\n\n\t/**\n\t* Size (in bytes) of each array element.\n\t*\n\t* @private\n\t* @name BYTES_PER_ELEMENT\n\t* @memberof StructArray.prototype\n\t* @readonly\n\t* @type {PositiveInteger}\n\t*/\n\tsetReadOnly( StructArray.prototype, 'BYTES_PER_ELEMENT', StructArray.BYTES_PER_ELEMENT );\n\n\t/**\n\t* Returns an array element.\n\t*\n\t* @private\n\t* @name get\n\t* @memberof StructArray.prototype\n\t* @type {Function}\n\t* @param {NonNegativeInteger} idx - element index\n\t* @throws {TypeError} `this` must be a struct array instance\n\t* @throws {TypeError} must provide a nonnegative integer\n\t* @returns {(*|void)} array element\n\t*/\n\tsetReadOnly( StructArray.prototype, 'get', function get( idx ) {\n\t\tif ( !isStructArray( this ) ) {\n\t\t\tthrow new TypeError( format( 'invalid invocation. `this` is not a %s.', CTOR_NAME ) );\n\t\t}\n\t\tif ( !isNonNegativeInteger( idx ) ) {\n\t\t\tthrow new TypeError( format( 'invalid argument. Must provide a nonnegative integer. Value: `%s`.', idx ) );\n\t\t}\n\t\tif ( idx >= this._length ) {\n\t\t\treturn;\n\t\t}\n\t\treturn new Struct( this._buffer, this._byteOffset+( idx*BYTES_PER_ELEMENT ), BYTES_PER_ELEMENT );\n\t});\n\n\t/**\n\t* Number of array elements.\n\t*\n\t* @private\n\t* @name length\n\t* @memberof StructArray.prototype\n\t* @readonly\n\t* @type {NonNegativeInteger}\n\t*/\n\tsetReadOnlyAccessor( StructArray.prototype, 'length', function get() {\n\t\treturn this._length;\n\t});\n\n\t/**\n\t* Sets an array element.\n\t*\n\t* ## Notes\n\t*\n\t* - When provided a struct array, we must check whether the source array shares the same buffer as the target array and whether the underlying memory overlaps. In particular, we are concerned with the following scenario:\n\t*\n\t* ```text\n\t* buf: ---------------------\n\t* src: ---------------------\n\t* ```\n\t*\n\t* In the above, as we copy values from `src`, we will overwrite values in the `src` view, resulting in duplicated values copied into the end of `buf`, which is not intended. Hence, to avoid overwriting source values, we must **copy** source values to a temporary array.\n\t*\n\t* In the other overlapping scenario,\n\t*\n\t* ```text\n\t* buf: ---------------------\n\t* src: ---------------------\n\t* ```\n\t*\n\t* by the time we begin copying into the overlapping region, we are copying from the end of `src`, a non-overlapping region, which means we don't run the risk of copying copied values, rather than the original `src` values, as intended.\n\t*\n\t* @private\n\t* @name set\n\t* @memberof StructArray.prototype\n\t* @type {Function}\n\t* @param {(Collection|StructArray|Struct|Object)} value - value(s)\n\t* @param {NonNegativeInteger} [i=0] - element index at which to start writing values\n\t* @throws {TypeError} `this` must be a struct array instance\n\t* @throws {TypeError} index argument must be a nonnegative integer\n\t* @throws {RangeError} index argument is out-of-bounds\n\t* @throws {RangeError} target array lacks sufficient storage to accommodate source values\n\t* @throws {TypeError} must provide a valid object or a struct instance having the same layout as elements in the target array\n\t* @returns {void}\n\t*/\n\tsetReadOnly( StructArray.prototype, 'set', function set( value ) {\n\t\tvar bbytes;\n\t\tvar sbytes;\n\t\tvar sbuf;\n\t\tvar opts;\n\t\tvar idx;\n\t\tvar buf;\n\t\tvar tmp;\n\t\tvar get;\n\t\tvar nb;\n\t\tvar N;\n\t\tvar s;\n\t\tvar f;\n\t\tvar i;\n\t\tvar j;\n\t\tif ( !isStructArray( this ) ) {\n\t\t\tthrow new TypeError( format( 'invalid invocation. `this` is not a %s.', CTOR_NAME ) );\n\t\t}\n\t\tbuf = this._buffer;\n\t\tif ( arguments.length > 1 ) {\n\t\t\tidx = arguments[ 1 ];\n\t\t\tif ( !isNonNegativeInteger( idx ) ) {\n\t\t\t\tthrow new TypeError( format( 'invalid argument. Index argument must be a nonnegative integer. Value: `%s`.', idx ) );\n\t\t\t}\n\t\t} else {\n\t\t\tidx = 0;\n\t\t}\n\t\topts = {\n\t\t\t'format': 'layout'\n\t\t};\n\t\tnb = Struct.byteLength;\n\n\t\tif ( isCollection( value ) && !contains( FIELDS, 'length' ) ) { // note: when one of the fields is 'length', we always assume that a provided value with a 'length' property is a struct and/or data object as there doesn't seem to be a surefire way to distinguish such an object from a regular array-like object (including accessor arrays)\n\t\t\tN = value.length;\n\t\t\tif ( idx+N > this._length ) {\n\t\t\t\tthrow new RangeError( 'invalid arguments. Target array lacks sufficient storage to accommodate source values.' );\n\t\t\t}\n\t\t\tsbuf = value;\n\t\t\tif ( sbuf.get && sbuf.set ) {\n\t\t\t\tget = accessorGetter( 'default' );\n\t\t\t} else {\n\t\t\t\tget = getter( 'default' );\n\t\t\t}\n\t\t\t// Check for overlapping memory...\n\t\t\tj = this._byteOffset + (idx*BYTES_PER_ELEMENT);\n\t\t\tif (\n\t\t\t\tsbuf.buffer === buf &&\n\t\t\t\t(\n\t\t\t\t\tsbuf.byteOffset < j &&\n\t\t\t\t\tsbuf.byteOffset+sbuf.byteLength > j\n\t\t\t\t)\n\t\t\t) {\n\t\t\t\t// FIXME: add optimization when `value` is a StructArray sharing the same buffer and having the same layout; in which case, we can simply copy `src` bytes to a temporary array and then copy those bytes into the target array, without needing to intermediate struct instance materialization\n\n\t\t\t\t// We need to copy source values...\n\t\t\t\ttmp = [];\n\t\t\t\tfor ( i = 0; i < N; i++ ) {\n\t\t\t\t\ttmp.push( get( value, i ) );\n\t\t\t\t}\n\t\t\t\tsbuf = tmp;\n\t\t\t\tget = getter( 'default' );\n\t\t\t}\n\t\t\tfor ( i = 0; i < N; idx++, i++ ) {\n\t\t\t\tthis.set( get( sbuf, i ), idx ); // note: this likely isn't the most performant approach, but it avoids having to replicate branching logic for handling struct instances vs data objects\n\t\t\t}\n\t\t\treturn;\n\t\t}\n\t\tif ( idx >= this._length ) {\n\t\t\tthrow new RangeError( format( 'invalid argument. Index argument is out-of-bounds. Value: `%u`.', idx ) );\n\t\t}\n\t\tif ( !isObject( value ) ) {\n\t\t\tthrow new TypeError( format( 'invalid argument. Must provide either a valid object or a struct instance. Value: `%s`.', value ) );\n\t\t}\n\t\t// Check for a struct instance having a matching layout...\n\t\tif ( value.toString( opts ) === LAYOUT ) {\n\t\t\t// Explicitly copy the bytes of the input struct instance to the corresponding array element...\n\t\t\tsbuf = Struct.viewOf( value );\n\t\t\tsbytes = new Uint8Array( sbuf.buffer, sbuf.byteOffset, nb );\n\t\t\tbbytes = new Uint8Array( buf, this._byteOffset+( idx*BYTES_PER_ELEMENT ), nb );\n\t\t\tgcopy( nb, sbytes, 1, 0, bbytes, 1, 0 );\n\t\t\treturn;\n\t\t}\n\t\t// Create a struct instance view for the target element:\n\t\ts = new Struct( buf, this._byteOffset+( idx*BYTES_PER_ELEMENT ), nb );\n\n\t\t// Assign field values from the input object (accounting for both own and inherited properties)...\n\t\tfor ( i = 0; i < FIELDS.length; i++ ) {\n\t\t\tf = FIELDS[ i ];\n\t\t\tif ( hasProp( value, f ) ) {\n\t\t\t\ts[ f ] = value[ f ];\n\t\t\t}\n\t\t}\n\t});\n\n\t/**\n\t* Element constructor.\n\t*\n\t* @private\n\t* @name struct\n\t* @memberof StructArray.prototype\n\t* @readonly\n\t* @type {Function}\n\t*/\n\tsetReadOnly( StructArray.prototype, 'struct', Struct );\n\n\treturn StructArray;\n\n\t/**\n\t* Returns a boolean indicating if a value is a struct array.\n\t*\n\t* @private\n\t* @param {*} value - value to test\n\t* @returns {boolean} boolean indicating if a value is a struct array\n\t*/\n\tfunction isStructArray( value ) {\n\t\treturn (\n\t\t\ttypeof value === 'object' &&\n\t\t\tvalue !== null &&\n\t\t\t(\n\t\t\t\tvalue.constructor.name === CTOR_NAME ||\n\t\t\t\tisPrototypeOf( value, StructArray.prototype )\n\t\t\t) &&\n\t\t\tvalue.BYTES_PER_ELEMENT === BYTES_PER_ELEMENT\n\t\t);\n\t}\n}\n\n\n// EXPORTS //\n\nmodule.exports = factory;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2025 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Return a constructor for creating arrays having a fixed-width composite data type.\n*\n* @module @stdlib/array-struct-factory\n*\n* @example\n* var Complex128 = require( '@stdlib/complex-float64-ctor' );\n* var structFactory = require( '@stdlib/dstructs-struct' );\n* var factory = require( '@stdlib/array-struct-factory' );\n*\n* var schema1 = [\n* {\n* 'name': 're',\n* 'type': 'float64'\n* },\n* {\n* 'name': 'im',\n* 'type': 'float64'\n* }\n* ];\n*\n* // Create a struct constructor for storing real and imaginary components:\n* var Components = structFactory( schema1 );\n*\n* var schema2 = [\n* {\n* 'type': 'union',\n* 'fields': [\n* {\n* 'name': 'value',\n* 'type': 'complex128'\n* },\n* {\n* 'name': 'components',\n* 'type': Components\n* }\n* ]\n* }\n* ];\n*\n* // Create a struct constructor for storing a double-precision complex number:\n* var Complex128Struct = structFactory( schema2 );\n*\n* // Create an array constructor for storing complex numbers:\n* var Complex128Array = factory( Complex128Struct );\n*\n* // Create a new array:\n* var x = new Complex128Array( 10 );\n* // returns <StructArray>\n*\n* // Retrieve the first element:\n* var v1 = x.get( 0 );\n*\n* // Resolve the complex number stored within the first element:\n* var z1 = v1.value;\n* // returns <Complex128>[ 0.0, 0.0 ]\n*\n* // Resolve the individual real and imaginary components:\n* var z2 = v1.components;\n*\n* var re = z2.re;\n* // returns 0.0\n*\n* var im = z2.im;\n* // returns 0.0\n*\n* // Create a new complex number struct:\n* var z3 = new Complex128Struct({\n* 'value': new Complex128( 3.0, 5.0 )\n* });\n*\n* // Update the first element of the array:\n* x.set( z3, 0 );\n*\n* // As `v1` is a view on same memory as the first element, resolve the complex number stored within the element:\n* var z4 = v1.value;\n* // returns <Complex128>[ 3.0, 5.0 ]\n*/\n\n// MODULES //\n\nvar main = require( './main.js' );\n\n\n// EXPORTS //\n\nmodule.exports = main;\n"],
|
|
5
|
+
"mappings": "uGAAA,IAAAA,EAAAC,EAAA,SAAAC,GAAAC,EAAA,cAsBA,IAAIC,EAAS,QAAS,2BAA4B,EAC9CC,EAAiB,QAAS,oCAAqC,EAC/DC,EAAa,QAAS,qBAAsB,EAC5CC,EAAQ,QAAS,yBAA0B,EAAE,QA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["require_from_array", "__commonJSMin", "exports", "module", "getter", "accessorGetter", "Uint8Array", "gcopy", "fromArray", "Struct", "buf", "arr", "sbytes", "bbytes", "offset", "layout", "sview", "opts", "len", "get", "flg", "nb", "v", "i", "err", "require_from_iterator", "__commonJSMin", "exports", "module", "fromIterator", "it", "out", "require_main", "__commonJSMin", "exports", "module", "isStructConstructorLike", "isNonNegativeInteger", "isInteger", "isArrayBuffer", "isCollection", "isFunction", "isObject", "isPrototypeOf", "hasIteratorSymbolSupport", "hasProp", "contains", "setReadOnly", "setReadOnlyAccessor", "ITERATOR_SYMBOL", "ArrayBuffer", "Uint8Array", "getter", "accessorGetter", "gcopy", "structFactory", "format", "fromArray", "fromIterator", "HAS_ITERATOR_SYMBOL", "CTOR_NAME", "factory", "arg", "BYTES_PER_ELEMENT", "LAYOUT", "FIELDS", "Struct", "StructArray", "byteOffset", "length", "nargs", "buf", "len", "tmp", "idx", "isStructArray", "value", "bbytes", "sbytes", "sbuf", "opts", "get", "nb", "N", "s", "f", "i", "j", "main"]
|
|
7
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Apache-2.0
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2025 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
|
+
'use strict';
|
|
20
|
+
|
|
21
|
+
// MODULES //
|
|
22
|
+
|
|
23
|
+
var getter = require( '@stdlib/array-base-getter' );
|
|
24
|
+
var accessorGetter = require( '@stdlib/array-base-accessor-getter' );
|
|
25
|
+
var Uint8Array = require( '@stdlib/array-uint8' );
|
|
26
|
+
var gcopy = require( '@stdlib/blas-base-gcopy' ).ndarray;
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
// MAIN //
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Fills an output ArrayBuffer with array values.
|
|
33
|
+
*
|
|
34
|
+
* @private
|
|
35
|
+
* @param {Function} Struct - struct constructor
|
|
36
|
+
* @param {ArrayBuffer} buf - output data buffer
|
|
37
|
+
* @param {Collection} arr - input array
|
|
38
|
+
* @throws {TypeError} an input array must contain struct instances
|
|
39
|
+
* @throws {TypeError} each element of an input array must be a struct instance having the expected layout
|
|
40
|
+
* @returns {ArrayBuffer} output data buffer
|
|
41
|
+
*/
|
|
42
|
+
function fromArray( Struct, buf, arr ) {
|
|
43
|
+
var sbytes;
|
|
44
|
+
var bbytes;
|
|
45
|
+
var offset;
|
|
46
|
+
var layout;
|
|
47
|
+
var sview;
|
|
48
|
+
var opts;
|
|
49
|
+
var len;
|
|
50
|
+
var get;
|
|
51
|
+
var flg;
|
|
52
|
+
var nb;
|
|
53
|
+
var v;
|
|
54
|
+
var i;
|
|
55
|
+
|
|
56
|
+
opts = {
|
|
57
|
+
'format': 'layout'
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
len = arr.length;
|
|
61
|
+
if ( arr.get && arr.set ) {
|
|
62
|
+
get = accessorGetter( 'default' );
|
|
63
|
+
} else {
|
|
64
|
+
get = getter( 'default' );
|
|
65
|
+
}
|
|
66
|
+
layout = Struct.layout;
|
|
67
|
+
nb = Struct.byteLength;
|
|
68
|
+
|
|
69
|
+
// FIXME: add optimization for when `buf` is a StructArray having the same layout, as can just copy bytes
|
|
70
|
+
|
|
71
|
+
bbytes = new Uint8Array( buf );
|
|
72
|
+
offset = 0;
|
|
73
|
+
for ( i = 0; i < len; i++ ) {
|
|
74
|
+
v = get( arr, i );
|
|
75
|
+
try {
|
|
76
|
+
sview = Struct.viewOf( v ); // note: this should throw if `v` is not a struct
|
|
77
|
+
flg = true;
|
|
78
|
+
} catch ( err ) { // eslint-disable-line no-unused-vars
|
|
79
|
+
try {
|
|
80
|
+
// Attempt to convert the input value to a struct instance:
|
|
81
|
+
v = new Struct( v ); // note: this should throw if `v` is not an object with valid fields
|
|
82
|
+
} catch ( err ) { // eslint-disable-line no-unused-vars
|
|
83
|
+
return null;
|
|
84
|
+
}
|
|
85
|
+
sview = Struct.viewOf( v );
|
|
86
|
+
flg = false;
|
|
87
|
+
}
|
|
88
|
+
if ( flg && v.toString( opts ) !== layout ) {
|
|
89
|
+
return null;
|
|
90
|
+
}
|
|
91
|
+
sbytes = new Uint8Array( sview.buffer, sview.byteOffset, sview.byteLength ); // eslint-disable-line max-len
|
|
92
|
+
gcopy( nb, sbytes, 1, 0, bbytes, 1, offset );
|
|
93
|
+
offset += nb;
|
|
94
|
+
}
|
|
95
|
+
return buf;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
// EXPORTS //
|
|
100
|
+
|
|
101
|
+
module.exports = fromArray;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Apache-2.0
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2025 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
|
+
'use strict';
|
|
20
|
+
|
|
21
|
+
// MAIN //
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Returns an array of iterated values.
|
|
25
|
+
*
|
|
26
|
+
* @private
|
|
27
|
+
* @param {Object} it - iterator
|
|
28
|
+
* @returns {Array} output array
|
|
29
|
+
*/
|
|
30
|
+
function fromIterator( it ) {
|
|
31
|
+
var out;
|
|
32
|
+
var v;
|
|
33
|
+
|
|
34
|
+
out = [];
|
|
35
|
+
while ( true ) {
|
|
36
|
+
v = it.next();
|
|
37
|
+
if ( v.done ) {
|
|
38
|
+
break;
|
|
39
|
+
}
|
|
40
|
+
out.push( v.value );
|
|
41
|
+
}
|
|
42
|
+
return out;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
// EXPORTS //
|
|
47
|
+
|
|
48
|
+
module.exports = fromIterator;
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Apache-2.0
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2025 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
|
+
'use strict';
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Return a constructor for creating arrays having a fixed-width composite data type.
|
|
23
|
+
*
|
|
24
|
+
* @module @stdlib/array-struct-factory
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* var Complex128 = require( '@stdlib/complex-float64-ctor' );
|
|
28
|
+
* var structFactory = require( '@stdlib/dstructs-struct' );
|
|
29
|
+
* var factory = require( '@stdlib/array-struct-factory' );
|
|
30
|
+
*
|
|
31
|
+
* var schema1 = [
|
|
32
|
+
* {
|
|
33
|
+
* 'name': 're',
|
|
34
|
+
* 'type': 'float64'
|
|
35
|
+
* },
|
|
36
|
+
* {
|
|
37
|
+
* 'name': 'im',
|
|
38
|
+
* 'type': 'float64'
|
|
39
|
+
* }
|
|
40
|
+
* ];
|
|
41
|
+
*
|
|
42
|
+
* // Create a struct constructor for storing real and imaginary components:
|
|
43
|
+
* var Components = structFactory( schema1 );
|
|
44
|
+
*
|
|
45
|
+
* var schema2 = [
|
|
46
|
+
* {
|
|
47
|
+
* 'type': 'union',
|
|
48
|
+
* 'fields': [
|
|
49
|
+
* {
|
|
50
|
+
* 'name': 'value',
|
|
51
|
+
* 'type': 'complex128'
|
|
52
|
+
* },
|
|
53
|
+
* {
|
|
54
|
+
* 'name': 'components',
|
|
55
|
+
* 'type': Components
|
|
56
|
+
* }
|
|
57
|
+
* ]
|
|
58
|
+
* }
|
|
59
|
+
* ];
|
|
60
|
+
*
|
|
61
|
+
* // Create a struct constructor for storing a double-precision complex number:
|
|
62
|
+
* var Complex128Struct = structFactory( schema2 );
|
|
63
|
+
*
|
|
64
|
+
* // Create an array constructor for storing complex numbers:
|
|
65
|
+
* var Complex128Array = factory( Complex128Struct );
|
|
66
|
+
*
|
|
67
|
+
* // Create a new array:
|
|
68
|
+
* var x = new Complex128Array( 10 );
|
|
69
|
+
* // returns <StructArray>
|
|
70
|
+
*
|
|
71
|
+
* // Retrieve the first element:
|
|
72
|
+
* var v1 = x.get( 0 );
|
|
73
|
+
*
|
|
74
|
+
* // Resolve the complex number stored within the first element:
|
|
75
|
+
* var z1 = v1.value;
|
|
76
|
+
* // returns <Complex128>[ 0.0, 0.0 ]
|
|
77
|
+
*
|
|
78
|
+
* // Resolve the individual real and imaginary components:
|
|
79
|
+
* var z2 = v1.components;
|
|
80
|
+
*
|
|
81
|
+
* var re = z2.re;
|
|
82
|
+
* // returns 0.0
|
|
83
|
+
*
|
|
84
|
+
* var im = z2.im;
|
|
85
|
+
* // returns 0.0
|
|
86
|
+
*
|
|
87
|
+
* // Create a new complex number struct:
|
|
88
|
+
* var z3 = new Complex128Struct({
|
|
89
|
+
* 'value': new Complex128( 3.0, 5.0 )
|
|
90
|
+
* });
|
|
91
|
+
*
|
|
92
|
+
* // Update the first element of the array:
|
|
93
|
+
* x.set( z3, 0 );
|
|
94
|
+
*
|
|
95
|
+
* // As `v1` is a view on same memory as the first element, resolve the complex number stored within the element:
|
|
96
|
+
* var z4 = v1.value;
|
|
97
|
+
* // returns <Complex128>[ 3.0, 5.0 ]
|
|
98
|
+
*/
|
|
99
|
+
|
|
100
|
+
// MODULES //
|
|
101
|
+
|
|
102
|
+
var main = require( './main.js' );
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
// EXPORTS //
|
|
106
|
+
|
|
107
|
+
module.exports = main;
|