|
| 1 | +/** |
| 2 | +* @license Apache-2.0 |
| 3 | +* |
| 4 | +* Copyright (c) 2024 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 normalizeSlice = require( '@stdlib/slice-base-normalize-slice' ); |
| 24 | +var sliceLength = require( '@stdlib/slice-base-length' ); |
| 25 | +var zeros = require( '@stdlib/array-base-zeros' ); |
| 26 | +var dtype = require( '@stdlib/array-dtype' ); |
| 27 | +var gcopy = require( '@stdlib/blas-base-gcopy' ).ndarray; |
| 28 | +var format = require( '@stdlib/string-format' ); |
| 29 | + |
| 30 | + |
| 31 | +// MODULES // |
| 32 | + |
| 33 | +/** |
| 34 | +* Returns a shallow copy of a portion of an array. |
| 35 | +* |
| 36 | +* @param {Collection} x - input array |
| 37 | +* @param {Slice} s - slice object |
| 38 | +* @param {boolean} strict - boolean indicating whether to enforce strict bounds checking |
| 39 | +* @throws {RangeError} slice exceeds array bounds |
| 40 | +* @returns {Collection} output array |
| 41 | +* |
| 42 | +* @example |
| 43 | +* var Slice = require( '@stdlib/slice-ctor' ); |
| 44 | +* |
| 45 | +* var x = [ 1, 2, 3, 4, 5, 6, 7, 8 ]; |
| 46 | +* |
| 47 | +* var s = new Slice( null, null, -2 ); |
| 48 | +* // returns <Slice> |
| 49 | +* |
| 50 | +* var y = slice( x, s, false ); |
| 51 | +* // returns [ 8, 6, 4, 2 ] |
| 52 | +* |
| 53 | +* var out = ( y === x ); |
| 54 | +* // returns false |
| 55 | +* |
| 56 | +* @example |
| 57 | +* var Int32Array = require( '@stdlib/array-int32' ); |
| 58 | +* var Slice = require( '@stdlib/slice-ctor' ); |
| 59 | +* |
| 60 | +* var x = new Int32Array( [ 1, 2, 3, 4, 5, 6, 7, 8 ] ); |
| 61 | +* |
| 62 | +* var s = new Slice( null, null, -2 ); |
| 63 | +* // returns <Slice> |
| 64 | +* |
| 65 | +* var y = slice( x, s, false ); |
| 66 | +* // returns <Int32Array>[ 8, 6, 4, 2 ] |
| 67 | +* |
| 68 | +* var out = ( y === x ); |
| 69 | +* // returns false |
| 70 | +*/ |
| 71 | +function slice( x, s, strict ) { |
| 72 | + var out; |
| 73 | + var len; |
| 74 | + var ns; |
| 75 | + var dt; |
| 76 | + var N; |
| 77 | + |
| 78 | + len = x.length; |
| 79 | + |
| 80 | + // Normalize the slice object base on the array length: |
| 81 | + ns = normalizeSlice( s, len, true ); |
| 82 | + |
| 83 | + // Check whether the slice exceeds the array bounds... |
| 84 | + if ( ns.code ) { |
| 85 | + if ( strict ) { |
| 86 | + throw new RangeError( format( 'invalid argument. Slice exceeds array bounds. Array length: %d.', len ) ); |
| 87 | + } |
| 88 | + // Normalize again, this time allowing for out-of-bounds indices: |
| 89 | + ns = normalizeSlice( s, len, false ); |
| 90 | + } |
| 91 | + // Compute the slice length: |
| 92 | + N = sliceLength( ns ); |
| 93 | + |
| 94 | + // Resolve the input array data type: |
| 95 | + dt = dtype( x ); |
| 96 | + |
| 97 | + // Allocate an output array: |
| 98 | + if ( dt === 'generic' || dt === null ) { // note: if we were provided an "exotic" array object, fallback to always returning a "generic" array |
| 99 | + out = zeros( N ); |
| 100 | + } else { |
| 101 | + out = new x.constructor( N ); // note: this should accommodate array species which inherit from built-in/known constructors and we assume that all constructors support providing a length argument |
| 102 | + } |
| 103 | + // Copy elements to the output array: |
| 104 | + gcopy( N, x, ns.step, ns.start, out, 1, 0 ); |
| 105 | + |
| 106 | + return out; |
| 107 | +} |
| 108 | + |
| 109 | + |
| 110 | +// EXPORTS // |
| 111 | + |
| 112 | +module.exports = slice; |
0 commit comments