Skip to content

Commit

Permalink
Auto-generated commit
Browse files Browse the repository at this point in the history
  • Loading branch information
stdlib-bot committed Sep 27, 2023
1 parent 985be96 commit 3a04a99
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 36 deletions.
3 changes: 1 addition & 2 deletions base/slice/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ var nonreducedDimensions = require( '@stdlib/slice/base/nonreduced-dimensions' )
var sliceShape = require( '@stdlib/slice/base/shape' );
var take = require( '@stdlib/array/base/take' );
var zeros = require( '@stdlib/array/base/zeros' );
var vind2bind = require( './../../../base/vind2bind' );
var numel = require( './../../../base/numel' );
var format = require( '@stdlib/string/format' );
var sliceStart = require( './slice_start.js' );
Expand Down Expand Up @@ -139,7 +138,7 @@ function slice( x, s, strict ) {
return empty( ctor, dtype, take( sh, sdims ), order );
}
// Resolve the index offset of the first element indexed by the slice:
offset = vind2bind( shape, strides, offset, order, sliceStart( ns, shape, strides, 0 ), 'throw' ); // TODO: @stdlib/ndarray/base/sind2bind
offset = sliceStart( ns, strides, offset ); // TODO: @stdlib/ndarray/base/sind2bind

// Remove reduced dimensions from the slice shape:
sh = take( sh, sdims );
Expand Down
27 changes: 8 additions & 19 deletions base/slice/lib/slice_start.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,39 +18,28 @@

'use strict';

// MODULES //

var sub2ind = require( './../../../base/sub2ind' );


// MAIN //

/**
* Resolves the linear index of the first element indexed by a normalized multi-slice.
*
* ## Notes
*
* - If `strides` contains negative strides, if an `offset` is greater than `0`, the function returns a linear index with respect to the underlying data buffer. If an `offset` is equal to `0`, the function returns a linear index with respect to the array view. For more information, see `@stdlib/ndarray/base/sub2ind`.
* Resolves the index offset of the first element indexed by a normalized multi-slice.
*
* @private
* @param {MultiSlice} slice - normalized multi-slice object
* @param {NonNegativeIntegerArray} shape - array shape
* @param {IntegerArray} strides - array strides
* @param {NonNegativeInteger} offset - index offset
* @returns {NonNegativeInteger} linear index of the first element
* @param {NonNegativeInteger} offset - array index offset
* @returns {NonNegativeInteger} index offset of the first element indexed by a normalized multi-slice object
*/
function sliceStart( slice, shape, strides, offset ) {
function sliceStart( slice, strides, offset ) {
var data;
var args;
var idx;
var i;

data = slice.data;
args = [ shape, strides, offset ];
idx = offset;
for ( i = 0; i < data.length; i++ ) {
args.push( data[ i ].start );
idx += strides[ i ] * data[ i ].start;
}
args.push( 'throw' );
return sub2ind.apply( null, args );
return idx;
}


Expand Down
49 changes: 49 additions & 0 deletions base/slice/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,3 +279,52 @@ tape( 'if all dimensions are reduced, the function returns a zero-dimensional ar
}
t.end();
});

tape( 'the function returns a view of a provided input array (ndims=1)', function test( t ) {
var expected;
var actual;
var buf;
var ord;
var sh;
var st;
var o;
var x;
var s;
var i;

buf = typedarray( zeroTo( 30 ), 'float64' );
sh = [ 6 ];
st = [ 2 ];
o = 4;
ord = 'row-major';

x = new ctor( 'float64', buf, sh, st, o, ord );

s = new MultiSlice( null );
actual = slice( x, s, true );

t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' );
t.strictEqual( actual.ndims, 1, 'returns expected value' );
t.strictEqual( actual.dtype, x.dtype, 'returns expected value' );
t.strictEqual( actual.data, x.data, 'returns expected value' );

expected = [ 4, 6, 8, 10, 12, 14 ];
for ( i = 0; i < expected.length; i++ ) {
t.strictEqual( actual.iget( i ), expected[ i ], 'returns expected value' );
}

// Reverse order and skip every other element:
s = new MultiSlice( new Slice( null, null, -2 ) );
actual = slice( x, s, true );

t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' );
t.strictEqual( actual.ndims, 1, 'returns expected value' );
t.strictEqual( actual.dtype, x.dtype, 'returns expected value' );
t.strictEqual( actual.data, x.data, 'returns expected value' );

expected = [ 14, 10, 6 ];
for ( i = 0; i < expected.length; i++ ) {
t.strictEqual( actual.iget( i ), expected[ i ], 'returns expected value' );
}
t.end();
});
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions dist/index.js.map

Large diffs are not rendered by default.

20 changes: 10 additions & 10 deletions fancy/examples/1d.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ var E = require( '@stdlib/slice/multi' );
var toArray = require( './../../to-array' );
var FancyArray = require( './../lib' );

var buffer = [ 1, 2, 3, 4, 5, 6 ];
var buffer = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 ];
var shape = [ 6 ];
var strides = [ 1 ];
var offset = 0;
var strides = [ 2 ];
var offset = 2;

var x = new FancyArray( 'generic', buffer, shape, strides, offset, 'row-major' );
// returns <FancyArray>
Expand All @@ -41,12 +41,12 @@ console.log( 'ndims: %d', ndims );
// Retrieve an ndarray element:
var v = x.get( 2 );
console.log( 'x[2] = %d', v );
// => 'x[2] = 3'
// => 'x[2] = 7'

// Set an ndarray element:
x.set( 2, 10 );
x.set( 2, 100 );
console.log( 'x[2] = %d', x.get( 2 ) );
// => 'x[2] = 10'
// => 'x[2] = 100'

// Create an alias for `undefined` for more concise slicing expressions:
var _ = void 0;
Expand All @@ -58,19 +58,19 @@ var s = E( S(0,_,2) );
// Use a multi-slice to create a view on the original ndarray:
var y1 = x[ s ];
console.log( toArray( y1 ) );
// => [ 1, 10, 5 ]
// => [ 3, 100, 11 ]

// Use a slice to create a view on the original ndarray:
var y2 = x[ s.data[0] ];
console.log( toArray( y2 ) );
// => [ 1, 10, 5 ]
// => [ 3, 100, 11 ]

// Use alternative syntax:
var y3 = x[ [ S(0,_,2) ] ];
console.log( toArray( y3 ) );
// => [ 1, 10, 5 ]
// => [ 3, 100, 11 ]

// Use alternative syntax:
var y4 = x[ '::-2' ];
console.log( toArray( y4 ) );
// => [ 6, 4, 2 ]
// => [ 13, 9, 5 ]
40 changes: 40 additions & 0 deletions test/dist/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2018 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 tape = require( 'tape' );
var objectKeys = require( '@stdlib/utils/keys' );
var ns = require( './../../dist' );


// TESTS //

tape( 'main export is an object', function test( t ) {
t.ok( true, __filename );
t.strictEqual( typeof ns, 'object', 'main export is an object' );
t.end();
});

tape( 'the exported object contains key-value pairs', function test( t ) {
var keys = objectKeys( ns );
t.equal( keys.length > 0, true, 'has keys' );
t.end();
});

0 comments on commit 3a04a99

Please sign in to comment.