Skip to content

Commit a580047

Browse files
committed
feat: add array/base/fancy-slice
1 parent 8c4e1d2 commit a580047

File tree

11 files changed

+1485
-0
lines changed

11 files changed

+1485
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2024 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# slice
22+
23+
> Return a shallow copy of a portion of an array.
24+
25+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
26+
27+
<section class="intro">
28+
29+
</section>
30+
31+
<!-- /.intro -->
32+
33+
<!-- Package usage documentation. -->
34+
35+
<section class="usage">
36+
37+
## Usage
38+
39+
```javascript
40+
var slice = require( '@stdlib/array/base/fancy-slice' );
41+
```
42+
43+
#### slice( x, s, strict )
44+
45+
Returns a shallow copy of a portion of an array.
46+
47+
```javascript
48+
var Slice = require( '@stdlib/slice/ctor' );
49+
50+
var x = [ 1, 2, 3, 4, 5, 6 ];
51+
52+
var s = new Slice( 1, 4 );
53+
var out = slice( x, s, false );
54+
// returns [ 2, 3, 4 ]
55+
56+
var bool = ( out === x );
57+
// returns false
58+
```
59+
60+
The function supports the following parameters:
61+
62+
- **x**: input array.
63+
- **s**: [slice][@stdlib/slice/ctor] object.
64+
- **strict**: boolean indicating whether to enforce strict bounds checking.
65+
66+
</section>
67+
68+
<!-- /.usage -->
69+
70+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
71+
72+
<section class="notes">
73+
74+
## Notes
75+
76+
- If provided an array-like object having an unknown [data type][@stdlib/array/dtype], the function copies input array elements to a new generic array.
77+
78+
</section>
79+
80+
<!-- /.notes -->
81+
82+
<!-- Package usage examples. -->
83+
84+
<section class="examples">
85+
86+
## Examples
87+
88+
<!-- eslint no-undef: "error" -->
89+
90+
```javascript
91+
var zeroTo = require( '@stdlib/array/zero-to' );
92+
var Slice = require( '@stdlib/slice/ctor' );
93+
var slice = require( '@stdlib/array/base/fancy-slice' );
94+
95+
var x = zeroTo( 10, 'generic' );
96+
// returns [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
97+
98+
var s = new Slice();
99+
var y = slice( x, s, false );
100+
// returns [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
101+
102+
s = new Slice( null, null, -2 );
103+
y = slice( x, s, false );
104+
// returns [ 9, 7, 5, 3, 1 ]
105+
106+
s = new Slice( -2, null, -2 );
107+
y = slice( x, s, false );
108+
// returns [ 8, 6, 4, 2, 0 ]
109+
110+
s = new Slice( 2, -2 );
111+
y = slice( x, s, false );
112+
// returns [ 2, 3, 4, 5, 6, 7 ]
113+
114+
s = new Slice( 2, 5 );
115+
y = slice( x, s, false );
116+
// returns [ 2, 3, 4 ]
117+
118+
s = new Slice( 4, 1, -1 );
119+
y = slice( x, s, false );
120+
// returns [ 4, 3, 2 ]
121+
122+
s = new Slice( 5 );
123+
y = slice( x, s, false );
124+
// returns [ 0, 1, 2, 3, 4 ]
125+
126+
s = new Slice( 5, null );
127+
y = slice( x, s, false );
128+
// returns [ 5, 6, 7, 8, 9 ]
129+
```
130+
131+
</section>
132+
133+
<!-- /.examples -->
134+
135+
<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
136+
137+
<section class="references">
138+
139+
</section>
140+
141+
<!-- /.references -->
142+
143+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
144+
145+
<section class="related">
146+
147+
</section>
148+
149+
<!-- /.related -->
150+
151+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
152+
153+
<section class="links">
154+
155+
[@stdlib/slice/ctor]: https://github.com/stdlib-js/stdlib
156+
157+
[@stdlib/array/dtype]: https://github.com/stdlib-js/stdlib
158+
159+
</section>
160+
161+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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 bench = require( '@stdlib/bench' );
24+
var pow = require( '@stdlib/math/base/special/pow' );
25+
var isCollection = require( '@stdlib/assert/is-collection' );
26+
var zeroTo = require( '@stdlib/array/zero-to' );
27+
var Slice = require( '@stdlib/slice/ctor' );
28+
var pkg = require( './../package.json' ).name;
29+
var slice = 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 x = zeroTo( len, 'float64' );
43+
return benchmark;
44+
45+
/**
46+
* Benchmark function.
47+
*
48+
* @private
49+
* @param {Benchmark} b - benchmark instance
50+
*/
51+
function benchmark( b ) {
52+
var values;
53+
var out;
54+
var i;
55+
56+
values = [
57+
new Slice(),
58+
new Slice(),
59+
new Slice()
60+
];
61+
62+
b.tic();
63+
for ( i = 0; i < b.iterations; i++ ) {
64+
out = slice( x, values[ i%values.length ] );
65+
if ( out.length !== len ) {
66+
b.fail( 'unexpected length' );
67+
}
68+
}
69+
b.toc();
70+
if ( !isCollection( out ) ) {
71+
b.fail( 'should return an array' );
72+
}
73+
b.pass( 'benchmark finished' );
74+
b.end();
75+
}
76+
}
77+
78+
79+
// MAIN //
80+
81+
/**
82+
* Main execution sequence.
83+
*
84+
* @private
85+
*/
86+
function main() {
87+
var len;
88+
var min;
89+
var max;
90+
var f;
91+
var i;
92+
93+
min = 1; // 10^min
94+
max = 6; // 10^max
95+
96+
for ( i = min; i <= max; i++ ) {
97+
len = pow( 10, i );
98+
99+
f = createBenchmark( len );
100+
bench( pkg+':dtype=float64,len='+len, f );
101+
}
102+
}
103+
104+
main();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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 bench = require( '@stdlib/bench' );
24+
var pow = require( '@stdlib/math/base/special/pow' );
25+
var isCollection = require( '@stdlib/assert/is-collection' );
26+
var zeroTo = require( '@stdlib/array/zero-to' );
27+
var Slice = require( '@stdlib/slice/ctor' );
28+
var pkg = require( './../package.json' ).name;
29+
var slice = 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 x = zeroTo( len, 'generic' );
43+
return benchmark;
44+
45+
/**
46+
* Benchmark function.
47+
*
48+
* @private
49+
* @param {Benchmark} b - benchmark instance
50+
*/
51+
function benchmark( b ) {
52+
var values;
53+
var out;
54+
var i;
55+
56+
values = [
57+
new Slice(),
58+
new Slice(),
59+
new Slice()
60+
];
61+
62+
b.tic();
63+
for ( i = 0; i < b.iterations; i++ ) {
64+
out = slice( x, values[ i%values.length ] );
65+
if ( out.length !== len ) {
66+
b.fail( 'unexpected length' );
67+
}
68+
}
69+
b.toc();
70+
if ( !isCollection( out ) ) {
71+
b.fail( 'should return an array' );
72+
}
73+
b.pass( 'benchmark finished' );
74+
b.end();
75+
}
76+
}
77+
78+
79+
// MAIN //
80+
81+
/**
82+
* Main execution sequence.
83+
*
84+
* @private
85+
*/
86+
function main() {
87+
var len;
88+
var min;
89+
var max;
90+
var f;
91+
var i;
92+
93+
min = 1; // 10^min
94+
max = 6; // 10^max
95+
96+
for ( i = min; i <= max; i++ ) {
97+
len = pow( 10, i );
98+
99+
f = createBenchmark( len );
100+
bench( pkg+':dtype=generic,len='+len, f );
101+
}
102+
}
103+
104+
main();

0 commit comments

Comments
 (0)