Skip to content

Commit 836d0f2

Browse files
authored
feat: add join method to array/complex128
PR-URL: #1252 Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent 4007945 commit 836d0f2

File tree

6 files changed

+411
-0
lines changed

6 files changed

+411
-0
lines changed

lib/node_modules/@stdlib/array/complex128/README.md

+30
Original file line numberDiff line numberDiff line change
@@ -1475,6 +1475,36 @@ idx = arr.indexOf( new Complex128( 1.0, -1.0 ), 1 );
14751475
// returns -1
14761476
```
14771477

1478+
<a name="method-join"></a>
1479+
1480+
#### Complex128Array.prototype.join( \[separator] )
1481+
1482+
Returns a new string by concatenating all array elements.
1483+
1484+
```javascript
1485+
var arr = new Complex128Array( 3 );
1486+
1487+
arr.set( [ 1.0, 1.0 ], 0 );
1488+
arr.set( [ 2.0, -2.0 ], 1 );
1489+
arr.set( [ 3.0, 3.0 ], 2 );
1490+
1491+
var str = arr.join();
1492+
// returns '1 + 1i,2 - 2i,3 + 3i'
1493+
```
1494+
1495+
By default, the method separates serialized array elements with a comma. To use an alternative separator, provide a `separator` string.
1496+
1497+
```javascript
1498+
var arr = new Complex128Array( 3 );
1499+
1500+
arr.set( [ 1.0, 1.0 ], 0 );
1501+
arr.set( [ 2.0, -2.0 ], 1 );
1502+
arr.set( [ 3.0, 3.0 ], 2 );
1503+
1504+
var str = arr.join( '/' );
1505+
// returns '1 + 1i/2 - 2i/3 + 3i'
1506+
```
1507+
14781508
<a name="method-last-index-of"></a>
14791509

14801510
#### Complex128Array.prototype.lastIndexOf( searchElement\[, fromIndex] )
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 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 pkg = require( './../package.json' ).name;
25+
var Complex128Array = require( './../lib' );
26+
27+
28+
// MAIN //
29+
30+
bench( pkg+':join', function benchmark( b ) {
31+
var out;
32+
var arr;
33+
var i;
34+
35+
arr = new Complex128Array( [ 1, 2, 3, 4, 5, 6 ] );
36+
37+
b.tic();
38+
for ( i = 0; i < b.iterations; i++ ) {
39+
out = arr.join();
40+
if ( typeof out !== 'string' ) {
41+
b.fail( 'should return a string' );
42+
}
43+
}
44+
b.toc();
45+
if ( typeof out !== 'string' ) {
46+
b.fail( 'should return a string' );
47+
}
48+
b.pass( 'benchmark finished' );
49+
b.end();
50+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 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 Complex128 = require( '@stdlib/complex/float64' );
26+
var pkg = require( './../package.json' ).name;
27+
var Complex128Array = require( './../lib' );
28+
29+
30+
// FUNCTIONS //
31+
32+
/**
33+
* Creates a benchmark function.
34+
*
35+
* @private
36+
* @param {PositiveInteger} len - array length
37+
* @returns {Function} benchmark function
38+
*/
39+
function createBenchmark( len ) {
40+
var arr;
41+
var i;
42+
43+
arr = [];
44+
for ( i = 0; i < len; i++ ) {
45+
arr.push( new Complex128( i, i ) );
46+
}
47+
arr = new Complex128Array( arr );
48+
49+
return benchmark;
50+
51+
/**
52+
* Benchmark function.
53+
*
54+
* @private
55+
* @param {Benchmark} b - benchmark instance
56+
*/
57+
function benchmark( b ) {
58+
var out;
59+
var i;
60+
61+
b.tic();
62+
for ( i = 0; i < b.iterations; i++ ) {
63+
out = arr.join( '/' );
64+
if ( typeof out !== 'string' ) {
65+
b.fail( 'should return a string' );
66+
}
67+
}
68+
b.toc();
69+
if ( typeof out !== 'string' ) {
70+
b.fail( 'should return a string' );
71+
}
72+
b.pass( 'benchmark finished' );
73+
b.end();
74+
}
75+
}
76+
77+
78+
// MAIN //
79+
80+
/**
81+
* Main execution sequence.
82+
*
83+
* @private
84+
*/
85+
function main() {
86+
var len;
87+
var min;
88+
var max;
89+
var f;
90+
var i;
91+
92+
min = 1; // 10^min
93+
max = 6; // 10^max
94+
95+
for ( i = min; i <= max; i++ ) {
96+
len = pow( 10, i );
97+
f = createBenchmark( len );
98+
bench( pkg+':join:len='+len, f );
99+
}
100+
}
101+
102+
main();

lib/node_modules/@stdlib/array/complex128/docs/types/index.d.ts

+20
Original file line numberDiff line numberDiff line change
@@ -726,6 +726,26 @@ declare class Complex128Array implements Complex128ArrayInterface {
726726
*/
727727
indexOf( searchElement: ComplexLike, fromIndex?: number ): number;
728728

729+
/**
730+
* Returns a new string by concatenating all array elements.
731+
*
732+
* @param separator - value separator (default: ',')
733+
* @returns string
734+
*
735+
* @example
736+
* var arr = new Complex128Array( 2 );
737+
*
738+
* arr.set( [ 1.0, 1.0 ], 0 );
739+
* arr.set( [ 2.0, 2.0 ], 1 );
740+
*
741+
* var str = arr.join();
742+
* // returns '1 + 1i,2 + 2i'
743+
*
744+
* str = arr.join( '/' );
745+
* // returns '1 + 1i/2 + 2i'
746+
*/
747+
join( separator?: string ): string;
748+
729749
/**
730750
* Returns the last index at which a given element can be found.
731751
*

lib/node_modules/@stdlib/array/complex128/lib/main.js

+47
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ var isCollection = require( '@stdlib/assert/is-collection' );
2828
var isArrayBuffer = require( '@stdlib/assert/is-arraybuffer' );
2929
var isObject = require( '@stdlib/assert/is-object' );
3030
var isArray = require( '@stdlib/assert/is-array' );
31+
var isString = require( '@stdlib/assert/is-string' );
3132
var isFunction = require( '@stdlib/assert/is-function' );
3233
var isComplexLike = require( '@stdlib/assert/is-complex-like' );
3334
var isEven = require( '@stdlib/math/base/assert/is-even' );
@@ -1537,6 +1538,52 @@ setReadOnly( Complex128Array.prototype, 'indexOf', function indexOf( searchEleme
15371538
return -1;
15381539
});
15391540

1541+
/**
1542+
* Returns a new string by concatenating all array elements.
1543+
*
1544+
* @name join
1545+
* @memberof Complex128Array.prototype
1546+
* @type {Function}
1547+
* @param {string} [separator=','] - element separator
1548+
* @throws {TypeError} `this` must be a complex number array
1549+
* @throws {TypeError} first argument must be a string
1550+
* @returns {string} string representation
1551+
*
1552+
* @example
1553+
* var arr = new Complex128Array( 2 );
1554+
*
1555+
* arr.set( [ 1.0, 1.0 ], 0 );
1556+
* arr.set( [ 2.0, 2.0 ], 1 );
1557+
*
1558+
* var str = arr.join();
1559+
* // returns '1 + 1i,2 + 2i'
1560+
*
1561+
* str = arr.join( '/' );
1562+
* // returns '1 + 1i/2 + 2i'
1563+
*/
1564+
setReadOnly( Complex128Array.prototype, 'join', function join( separator ) {
1565+
var out;
1566+
var buf;
1567+
var sep;
1568+
var i;
1569+
if ( !isComplexArray( this ) ) {
1570+
throw new TypeError( 'invalid invocation. `this` is not a complex number array.' );
1571+
}
1572+
if ( arguments.length === 0 ) {
1573+
sep = ',';
1574+
} else if ( isString( separator ) ) {
1575+
sep = separator;
1576+
} else {
1577+
throw new TypeError( format( 'invalid argument. First argument must be a string. Value: `%s`.', separator ) );
1578+
}
1579+
out = [];
1580+
buf = this._buffer;
1581+
for ( i = 0; i < this._length; i++ ) {
1582+
out.push( getComplex128( buf, i ).toString() );
1583+
}
1584+
return out.join( sep );
1585+
});
1586+
15401587
/**
15411588
* Returns the last index at which a given element can be found.
15421589
*

0 commit comments

Comments
 (0)