All files / concat1d/lib assign.js

48.97% Statements 72/147
100% Branches 1/1
0% Functions 0/1
48.97% Lines 72/147

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 1481x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                                                                                                                                                       1x 1x 1x 1x 1x  
/**
* @license Apache-2.0
*
* Copyright (c) 2025 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*    http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
 
'use strict';
 
// MODULES //
 
var isArrayLikeObject = require( '@stdlib/assert/is-array-like-object' );
var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' );
var broadcastScalar = require( '@stdlib/ndarray/base/broadcast-scalar' );
var broadcastArray = require( '@stdlib/ndarray/base/broadcast-array' );
var ndims = require( '@stdlib/ndarray/base/ndims' );
var defaults = require( '@stdlib/ndarray/defaults' );
var concat = require( '@stdlib/ndarray/concat' );
var format = require( '@stdlib/string/format' );
var resolveDataTypes = require( './resolve_dtypes.js' );
var resolveOrder = require( './resolve_order.js' );
 
 
// VARIABLES //
 
var DEFAULT_ORDER = defaults.get( 'order' );
var DEFAULT_DTYPE = defaults.get( 'dtypes.default' );
 
 
// MAIN //
 
/**
* Concatenates provided input arguments and assigns the result to a provided one-dimensional output ndarray.
*
* @param {...*} arguments - input arguments
* @param {ndarrayLike} out - output ndarray
* @throws {Error} must provide at least two arguments
* @throws {TypeError} ndarray arguments must have fewer than two dimensions
* @throws {Error} last argument must be a one-dimensional ndarray
* @returns {ndarray} output ndarray
*
* @example
* var array = require( '@stdlib/ndarray/array' );
* var empty = require( '@stdlib/ndarray/empty' );
* var ndarray2array = require( '@stdlib/ndarray/to-array' );
*
* var x = array( [ -1.0, 2.0, 3.0, 4.0 ] );
* var y = array( [ -5.0, 6.0, -7.0, -8.0, 9.0, -10.0 ] );
* var z = empty( [ 10 ] );
*
* var out = assign( x, y, z );
* // returns <ndarray>[ -1.0, 2.0, 3.0, 4.0, -5.0, 6.0, -7.0, -8.0, 9.0, -10.0 ]
*
* var bool = ( out === z );
* // returns true
*/
function assign() {
	var nargs;
	var args;
	var arrs;
	var isnd;
	var dims;
	var ord;
	var arg;
	var out;
	var dt;
	var i;
	var d;

	nargs = arguments.length;

	// Resolve function arguments...
	if ( nargs < 2 ) {
		throw new Error( 'invalid invocation. Must provide at least two arguments.' );
	}
	arg = arguments[ 0 ];
	if ( nargs === 2 ) {
		if ( isArrayLikeObject( arg ) && !isndarrayLike( arg ) ) {
			// Always copy inputs to a "generic" array...
			args = [];
			for ( i = 0; i < arg.length; i++ ) {
				args.push( arg[ i ] );
			}
		} else {
			args = [ arg ];
		}
	} else { // nargs > 2
		args = [];
		for ( i = 0; i < nargs - 1; i++ ) {
			args.push( arguments[ i ] );
		}
	}
	out = arguments[ nargs-1 ];
	if ( !isndarrayLike( out ) || ndims( out ) !== 1 ) {
		throw new TypeError( format( 'invalid argument. Last argument must be a one-dimensional ndarray. Value: `%s`.', out ) );
	}
	isnd = [];
	dims = [];
	arrs = [];
	for ( i = 0; i < args.length; i++ ) {
		isnd.push( isndarrayLike( args[ i ] ) );
		if ( isnd[ i ] ) {
			d = ndims( args[ i ] );
			if ( d > 1 ) {
				throw new TypeError( format( 'invalid argument. Each ndarray argument must have fewer than two dimensions. Argument: %d.', i ) );
			}
			dims.push( d );
			arrs.push( args[ i ] );
		} else {
			dims.push( -1 );
		}
	}
	if ( arrs.length >= 1 ) {
		dt = resolveDataTypes( arrs );
		ord = resolveOrder( arrs );
	} else {
		dt = DEFAULT_DTYPE;
		ord = DEFAULT_ORDER;
	}
	// Broadcast scalar or 0d ndarray inputs...
	for ( i = 0; i < args.length; i++ ) {
		if ( isnd[ i ] ) {
			if ( dims[ i ] === 0 ) {
				args[ i ] = broadcastArray( args[ i ], [ 1 ] );
			}
		} else {
			args[ i ] = broadcastScalar( args[ i ], dt, [ 1 ], ord );
		}
	}
	return concat.assign( args, out );
}
 
 
// EXPORTS //
 
module.exports = assign;