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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 | 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 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 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 198x 198x 198x 198x 198x 198x 198x 198x 198x 198x 198x 198x 198x 198x 198x 198x 198x 198x 198x 198x 198x 198x 198x 40x 40x 158x 158x 158x 158x 158x 158x 198x 2x 2x 156x 156x 156x 156x 156x 156x 156x 198x 84x 84x 84x 3x 3x 3x 6x 6x 3x 3x 3x 6x 6x 3x 84x 44x 44x 32x 32x 32x 32x 31x 31x 44x 1x 1x 1x 1x 1x 1x 12x 11x 11x 22x 22x 11x 81x 37x 37x 36x 36x 37x 198x 72x 72x 53x 53x 20x 20x 20x 1x 20x 19x 19x 19x 19x 19x 53x 6x 6x 6x 6x 6x 6x 6x 6x 10x 10x 6x 33x 27x 27x 62x 62x 27x 72x 19x 19x 18x 18x 19x 72x 156x 198x 4x 4x 152x 152x 198x 83x 44x 44x 20x 20x 44x 83x 132x 132x 132x 198x 4x 4x 128x 198x 68x 8x 8x 68x 42x 42x 198x 18x 18x 18x 18x 36x 36x 18x 6x 6x 12x 12x 6x 198x 24x 24x 12x 12x 24x 42x 42x 198x 18x 18x 6x 18x 6x 6x 18x 36x 36x 36x 198x 48x 48x 28x 28x 28x 28x 28x 48x 24x 24x 48x 48x 198x 24x 24x 24x 24x 24x 198x 36x 198x 1x 1x 1x 1x 1x | /**
* @license Apache-2.0
*
* Copyright (c) 2026 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.
*/
/* eslint-disable max-lines-per-function, max-statements */
'use strict';
// MODULES //
var isArray = require( '@stdlib/assert/is-array' );
var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' );
var isInteger = require( '@stdlib/assert/is-integer' );
var isPlainObject = require( '@stdlib/assert/is-plain-object' );
var hasOwnProp = require( '@stdlib/assert/has-own-property' );
var zeroTo = require( '@stdlib/array/zero-to' );
var Slice = require( '@stdlib/slice/ctor' );
var ndims = require( '@stdlib/ndarray/ndims' );
var normalizeIndex = require( '@stdlib/ndarray/base/normalize-index' );
var format = require( '@stdlib/string/format' );
var concat = require( '@stdlib/ndarray/concat' );
var args2multislice = require( '@stdlib/slice/base/args2multislice' );
var slice = require( '@stdlib/ndarray/base/slice' );
var getShape = require( '@stdlib/ndarray/shape' );
var getDType = require( '@stdlib/ndarray/dtype' );
var getOrder = require( '@stdlib/ndarray/order' );
var nulls = require( '@stdlib/array/base/nulls' );
var empty = require( '@stdlib/ndarray/empty' );
var normalizeSlices = require( './normalizeslices.js' );
// FUNCTIONS //
/**
* Returns a function that compares slice indices by their `start` value.
*
* @private
* @param {Array} slices - an array of slice objects.
* @returns {Function} - a function that compares slice indices by their `start` value.
*/
function sliceOrder( slices ) {
return fcn;
/**
* Compares two slice indices based on their `start` value.
*
* @private
* @param {number} i - index of the first slice
* @param {number} j - index of the second slice
* @returns {number} sort order
*/
function fcn( i, j ) {
return slices[ i ].start - slices[ j ].start;
}
}
// MAIN //
/**
* Returns an ndarray where the elements of an input ndarray are replaced or removed along a specific dimension.
*
* @param {ndarray} x - input array
* @param {...*} s - slice arguments
* @param {(ndarray|Array<ndarray>)} values - an ndarray or an array of ndarrays containing the elements to insert
* @param {Options} [options] - options
* @param {integer} [options.dim=-1] - dimension along which to perform the operation
* @throws {TypeError} first argument must be an ndarray
* @throws {TypeError} first argument must be an ndarray having one or more dimensions
* @throws {RangeError} dimension index exceeds the number of dimensions
* @throws {RangeError} slice exceeds array bounds
* @throws {TypeError} values argument must be an ndarray or an array of ndarrays
* @throws {RangeError} number of slices and values ndarray must be equal
* @throws {TypeError} options argument must be an object
* @throws {TypeError} must provide valid options
* @returns {ndarray} output ndarray
*
* @example
* var Slice = require( '@stdlib/slice/ctor' );
* var array = require( '@stdlib/ndarray/array' );
*
* var x = array( [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] );
* // returns <ndarray>[ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ]
*
* var y = array( [ [ 10.0, 20.0 ] ] );
* // returns <ndarray>[ [ 10.0, 20.0 ] ]
*
* var s = new Slice( 1, 2, 1 );
* // returns <Slice>
*
* var out = splice( x, s, y, {
* 'dim': -2
* });
* // returns <ndarray>[ [ 1.0, 2.0 ], [ 10.0, 20.0 ], [ 5.0, 6.0 ] ]
*/
function splice( x ) {
var sortedSlices;
var hasValues;
var options;
var hasOpts;
var indices;
var prevEnd;
var slices;
var pieces;
var nargs;
var spdim;
var opts;
var args;
var sh;
var ms;
var v1;
var s1;
var v;
var N;
var i;
nargs = arguments.length;
if ( !isndarrayLike( x ) ) {
throw new TypeError( format( 'invalid argument. First argument must be an ndarray. Value: `%s`.', x ) );
}
// Retrieve array meta data:
N = ndims( x );
sh = getShape( x );
// Check whether we were provided a zero-dimensional array...
if ( N === 0 ) {
throw new TypeError( format( 'invalid argument. First argument must be an ndarray having one or more dimensions. Number of dimensions: %d.', N ) );
}
// Resolve function arguments:
hasOpts = false;
hasValues = false;
opts = {
'dim': -1
};
if ( isPlainObject( arguments[ nargs - 1 ] ) ) {
hasOpts = true;
options = arguments[ nargs - 1 ];
if ( nargs > 4 ) {
v = arguments[ nargs - 2 ];
if ( isArray( v ) ) { // Case: splice( x, s1, s2, [ v1, v2 ], options )
for ( i = 0; i < v.length; i++ ) {
if ( !isndarrayLike( v[ i ] ) ) {
throw new TypeError( format( 'invalid argument. Values argument must be an array of ndarrays. Value: `%s`.', v ) );
}
}
hasValues = true;
slices = [];
for ( i = 1; i < arguments.length-2; i++ ) {
slices.push( arguments[ i ] );
}
} else { // Case: splice( x, s1, s2, s3, options )
slices = [];
for ( i = 1; i <= arguments.length - 2; i++ ) {
slices.push( arguments[ i ] );
}
}
} else if ( nargs === 4 ) {
v = arguments[ nargs - 2 ];
if ( isndarrayLike( v ) ) { // Case: splice( x, s1, v1, options ) or splice( x, [ s1 ], v1, options )
v = [ v ];
hasValues = true;
slices = arguments[ nargs - 3 ];
if ( !isArray( slices ) ) {
slices = [ arguments[ nargs - 3 ] ];
}
} else if ( isArray( v ) ) { // Case: splice( x, s1, [ v1 ], options ) or splice( x, [ s1 ], [ v1 ], options )
for ( i = 0; i < v.length; i++ ) {
if ( !isndarrayLike( v[ i ] ) ) {
throw new TypeError( format( 'invalid argument. Values argument must be an array of ndarrays. Value: `%s`.', v ) );
}
}
hasValues = true;
slices = arguments[ nargs - 3 ];
if ( !isArray( slices ) ) {
slices = [ arguments[ nargs - 3 ] ];
}
} else { // Case: splice( x, s1, s2, options )
slices = [];
for ( i = 1; i <= arguments.length - 2; i++ ) {
slices.push( arguments[ i ] );
}
}
} else if ( nargs <= 3 ) { // Case: splice( x, ???, options )
slices = arguments[ nargs - 2 ];
if ( !isArray( slices ) ) {
slices = [ slices ];
}
}
} else {
hasOpts = false;
if ( nargs >= 3 ) {
v = arguments[ nargs - 1 ];
if ( isndarrayLike( v ) ) { // Case: splice( x, s1, v1 ) or splice( x, [ s1 ], v1 )
v = [ v ];
hasValues = true;
if ( isArray( arguments[ 1 ] ) ) {
slices = arguments[ 1 ];
} else {
slices = [];
for ( i = 1; i < arguments.length-1; i++ ) {
slices.push( arguments[ i ] );
}
}
} else if ( isArray( v ) ) { // Case: splice( x, s1, s2, s3, [ v1, v2, v3 ] )
for ( i = 0; i < v.length; i++ ) {
if ( !isndarrayLike( v[ i ] ) ) {
throw new TypeError( format( 'invalid argument. Values argument must be an array of ndarrays. Value: `%s`.', v ) );
}
}
hasValues = true;
if ( isArray( arguments[ 1 ] ) ) {
slices = arguments[ 1 ];
} else {
slices = [];
for ( i = 1; i < arguments.length-1; i++ ) {
slices.push( arguments[ i ] );
}
}
} else { // Case: splice( x, s1, s2, s3 )
slices = [];
for ( i = 1; i < arguments.length; i++ ) {
slices.push( arguments[ i ] );
}
}
} else { // Case: splice( x, s1 ) or splice( x, [ s1 ] )
slices = arguments[ nargs - 1 ];
if ( !isArray( slices ) ) {
slices = [ arguments[ nargs - 1 ] ];
}
}
}
if ( hasValues && slices.length !== v.length ) {
throw new RangeError( 'invalid argument. Number of slices must be equal to the number of values ndarray.' );
}
// Validate options:
if ( hasOpts ) {
if ( hasOwnProp( options, 'dim') ) {
opts.dim = options.dim;
if ( !isInteger( opts.dim ) ) {
throw new TypeError( format( 'invalid option. `%s` option must be an integer. Option: `%d`.', 'dim', opts.dim ) );
}
}
}
// Normalize slices:
spdim = normalizeIndex( opts.dim, N-1 );
if ( spdim === -1 ) {
throw new RangeError( format( 'invalid argument. Dimension index exceeds the number of dimensions. Number of dimensions: %d. Value: `%d`.', N, spdim ) );
}
slices = normalizeSlices( x, slices, spdim );
for ( i = 0; i < slices.length; i++ ) {
if ( slices[ i ].step !== 1 ) {
throw new RangeError( format( 'invalid argument. Slice arguments must have an index increment of `1` . Value: `%d`.', slices[ i ].step ) );
}
}
// Sort the normalized slices:
if ( slices.length > 1 ) {
indices = zeroTo( slices.length );
indices.sort( sliceOrder( slices ) );
sortedSlices = [];
for ( i = 0; i < indices.length; i++ ) {
sortedSlices.push( slices[ indices[ i ] ] );
}
if ( hasValues ) {
v1 = [];
for ( i = 0; i < indices.length; i++ ) {
v1.push( v[ indices[ i ] ] );
}
}
} else {
sortedSlices = slices;
if ( hasValues ) {
v1 = v;
}
}
// Validate slice regions:
for ( i = 1; i < sortedSlices.length; i++ ) {
if (
slices[ i ].start < slices[ i - 1 ].stop &&
slices[ i ].stop > slices[ i - 1 ].start
) {
throw new RangeError( 'invalid argument. Slice regions must not overlap.' );
}
}
pieces = [];
prevEnd = 0;
for ( i = 0; i < sortedSlices.length; i++ ) {
s1 = sortedSlices[ i ];
if ( s1.start > prevEnd ) {
args = nulls( sh.length );
args[ spdim ] = new Slice( prevEnd, s1.start, 1 );
ms = args2multislice( args );
pieces.push( slice( x, ms, true, false ) );
}
if ( hasValues && v1[ i ] ) {
pieces.push( v1[ i ] );
}
prevEnd = s1.stop;
}
if ( prevEnd < sh[ spdim ] ) {
args = nulls( sh.length );
args[ spdim ] = new Slice( prevEnd, sh[ spdim ], 1 );
ms = args2multislice( args );
pieces.push( slice( x, ms, true, false ));
}
if ( pieces.length === 0 ) {
return empty( [], {
'dtype': getDType( x ),
'order': getOrder( x )
});
}
return concat( pieces, opts );
}
// EXPORTS //
module.exports = splice;
|