Skip to content

Commit 3a57fb4

Browse files
committed
feat: add assign method and add support for an index accessor array
1 parent 862cd8f commit 3a57fb4

13 files changed

Lines changed: 1711 additions & 66 deletions

File tree

lib/node_modules/@stdlib/array/base/take/README.md

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,36 +30,64 @@ limitations under the License.
3030
var take = require( '@stdlib/array/base/take' );
3131
```
3232

33-
#### take( x, indices )
33+
#### take( x, indices, mode )
3434

3535
Takes elements from an array.
3636

3737
```javascript
3838
var x = [ 1, 2, 3, 4 ];
3939

40-
var y = take( x, [ 1, 3 ] );
40+
var y = take( x, [ 1, 3 ], 'throw' );
4141
// returns [ 2, 4 ]
4242
```
4343

44+
The function supports the following parameters:
45+
46+
- **x**: input array.
47+
- **indices**: list of indices.
48+
- **mode**: index [mode][@stdlib/ndarray/base/ind].
49+
4450
If `indices` is an empty array, the function returns an empty array.
4551

4652
```javascript
4753
var x = [ 1, 2, 3, 4 ];
4854

49-
var y = take( x, [] );
55+
var y = take( x, [], 'throw' );
5056
// returns []
5157
```
5258

59+
#### take.assign( x, indices, mode, out, stride, offset )
60+
61+
Takes elements from an array and assigns the values to elements in a provided output array.
62+
63+
```javascript
64+
var x = [ 1, 2, 3, 4 ];
65+
66+
out = [ 0, 0, 0, 0, 0, 0 ];
67+
var indices = [ 0, 0, 1, 1, 3, 3 ];
68+
69+
var arr = take.assign( x, indices, 'throw', out, -1, out.length-1 );
70+
// returns [ 4, 4, 2, 2, 1, 1 ]
71+
72+
var bool = ( arr === out );
73+
// returns true
74+
```
75+
76+
The function supports the following parameters:
77+
78+
- **x**: input array.
79+
- **indices**: list of indices.
80+
- **mode**: index [mode][@stdlib/ndarray/base/ind].
81+
- **out**: output array.
82+
- **stride**: output array stride.
83+
- **offset**: output array offset.
84+
5385
</section>
5486

5587
<!-- /.usage -->
5688

5789
<section class="notes">
5890

59-
## Notes
60-
61-
- The function does **not** perform bounds checking. If an index is less than zero or greater than the maximum index of `x`, the value of the corresponding element in the output array is undefined.
62-
6391
</section>
6492

6593
<!-- /.notes -->
@@ -107,6 +135,8 @@ console.log( y );
107135

108136
<section class="links">
109137

138+
[@stdlib/ndarray/base/ind]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/ind
139+
110140
</section>
111141

112142
<!-- /.links -->
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2022 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 bench = require( '@stdlib/bench' );
24+
var pow = require( '@stdlib/math/base/special/pow' );
25+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
26+
var zeros = require( '@stdlib/array/zeros' );
27+
var isArray = require( '@stdlib/assert/is-array' );
28+
var pkg = require( './../package.json' ).name;
29+
var take = require( './../lib' );
30+
31+
32+
// FUNCTIONS //
33+
34+
/**
35+
* Creates a benchmark function.
36+
*
37+
* @private
38+
* @param {PositiveInteger} len - array length
39+
* @returns {Function} benchmark function
40+
*/
41+
function createBenchmark( len ) {
42+
var out;
43+
var idx;
44+
45+
idx = discreteUniform( len, 0, 3, {
46+
'dtype': 'generic'
47+
});
48+
out = zeros( len, 'generic' );
49+
50+
return benchmark;
51+
52+
/**
53+
* Benchmark function.
54+
*
55+
* @private
56+
* @param {Benchmark} b - benchmark instance
57+
*/
58+
function benchmark( b ) {
59+
var x;
60+
var v;
61+
var i;
62+
63+
x = [ 1, 2, 3, 4 ];
64+
65+
b.tic();
66+
for ( i = 0; i < b.iterations; i++ ) {
67+
v = take.assign( x, idx, 'throw', out, 1, 0 );
68+
if ( typeof v !== 'object' ) {
69+
b.fail( 'should return an array' );
70+
}
71+
}
72+
b.toc();
73+
if ( !isArray( v ) ) {
74+
b.fail( 'should return an array' );
75+
}
76+
b.pass( 'benchmark finished' );
77+
b.end();
78+
}
79+
}
80+
81+
82+
// MAIN //
83+
84+
/**
85+
* Main execution sequence.
86+
*
87+
* @private
88+
*/
89+
function main() {
90+
var len;
91+
var min;
92+
var max;
93+
var f;
94+
var i;
95+
96+
min = 1; // 10^min
97+
max = 6; // 10^max
98+
99+
for ( i = min; i <= max; i++ ) {
100+
len = pow( 10, i );
101+
f = createBenchmark( len );
102+
bench( pkg+':assign:len='+len, f );
103+
}
104+
}
105+
106+
main();

lib/node_modules/@stdlib/array/base/take/benchmark/benchmark.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ bench( pkg+'::copy:len=100', function benchmark( b ) {
3838

3939
b.tic();
4040
for ( i = 0; i < b.iterations; i++ ) {
41-
v = take( x, x );
41+
v = take( x, x, 'throw' );
4242
if ( typeof v !== 'object' ) {
4343
b.fail( 'should return an array' );
4444
}

lib/node_modules/@stdlib/array/base/take/benchmark/benchmark.length.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@
2222

2323
var bench = require( '@stdlib/bench' );
2424
var pow = require( '@stdlib/math/base/special/pow' );
25-
var filledBy = require( '@stdlib/array/base/filled-by' );
26-
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory;
25+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
2726
var isArray = require( '@stdlib/assert/is-array' );
2827
var pkg = require( './../package.json' ).name;
2928
var take = require( './../lib' );
@@ -39,7 +38,7 @@ var take = require( './../lib' );
3938
* @returns {Function} benchmark function
4039
*/
4140
function createBenchmark( len ) {
42-
var idx = filledBy( len, discreteUniform( 0, 3 ) );
41+
var idx = discreteUniform( len, 0, 3 );
4342
return benchmark;
4443

4544
/**
@@ -57,7 +56,7 @@ function createBenchmark( len ) {
5756

5857
b.tic();
5958
for ( i = 0; i < b.iterations; i++ ) {
60-
v = take( x, idx );
59+
v = take( x, idx, 'throw' );
6160
if ( typeof v !== 'object' ) {
6261
b.fail( 'should return an array' );
6362
}
Lines changed: 56 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11

2-
{{alias}}( x, indices )
2+
{{alias}}( x, indices, mode )
33
Takes elements from an array.
44

55
If `indices` is an empty array, the function returns an empty array.
66

7-
The function does *not* perform bounds checking. If an index is less than
8-
zero or greater than the maximum index of `x`, the value of the
9-
corresponding element in the output array is undefined.
10-
117
Parameters
128
----------
139
x: ArrayLikeObject
@@ -16,6 +12,15 @@
1612
indices: ArrayLikeObject<integer>
1713
List of element indices.
1814

15+
mode: string
16+
Specifies how to handle an index outside the interval [0, max], where
17+
`max` is the maximum possible array index. If equal to 'throw', the
18+
function throws an error. If equal to 'normalize', the function throws
19+
an error if provided an out-of-bounds normalized index. If equal to
20+
'wrap', the function wraps around an index using modulo arithmetic. If
21+
equal to 'clamp', the function sets an index to either 0 (minimum index)
22+
or the maximum index.
23+
1924
Returns
2025
-------
2126
out: Array
@@ -24,9 +29,54 @@
2429
Examples
2530
--------
2631
> var x = [ 1, 2, 3, 4 ];
27-
> var y = {{alias}}( x, [ 1, 3 ] )
32+
> var y = {{alias}}( x, [ 1, 3 ], 'throw' )
2833
[ 2, 4 ]
2934

35+
36+
{{alias}}.assign( x, indices, mode, out, stride, offset )
37+
Takes elements from an array and assigns the values to elements in a
38+
provided output array.
39+
40+
Parameters
41+
----------
42+
x: ArrayLikeObject
43+
Input array.
44+
45+
indices: ArrayLikeObject<integer>
46+
List of element indices.
47+
48+
mode: string
49+
Specifies how to handle an index outside the interval [0, max], where
50+
`max` is the maximum possible array index. If equal to 'throw', the
51+
function throws an error. If equal to 'normalize', the function throws
52+
an error if provided an out-of-bounds normalized index. If equal to
53+
'wrap', the function wraps around an index using modulo arithmetic. If
54+
equal to 'clamp', the function sets an index to either 0 (minimum index)
55+
or the maximum index.
56+
57+
out: ArrayLikeObject
58+
Output array.
59+
60+
stride: integer
61+
Output array stride.
62+
63+
offset: integer
64+
Output array offset.
65+
66+
Returns
67+
-------
68+
out: ArrayLikeObject
69+
Output array.
70+
71+
Examples
72+
--------
73+
> var x = [ 1, 2, 3, 4 ];
74+
> var out = [ 0, 0, 0, 0 ];
75+
> var arr = {{alias}}.assign( x, [ 1, 3 ], 'throw', out, 2, 0 )
76+
[ 2, 0, 4, 0 ]
77+
> var bool = ( arr === out )
78+
true
79+
3080
See Also
3181
--------
3282

0 commit comments

Comments
 (0)