diff --git a/lib/node_modules/@stdlib/iter/cartesian-power/README.md b/lib/node_modules/@stdlib/iter/cartesian-power/README.md new file mode 100644 index 000000000000..c74f257a8310 --- /dev/null +++ b/lib/node_modules/@stdlib/iter/cartesian-power/README.md @@ -0,0 +1,168 @@ + + +# iterCartesianPower + +> Create an iterator which returns the Cartesian power. + + + +
+ +
+ + + + + +
+ +## Usage + +```javascript +var iterCartesianPower = require( '@stdlib/iter/cartesian-power' ); +``` + +#### iterCartesianPower( x, n ) + +Returns an iterator which returns the Cartesian power. + +```javascript +var iterator = iterCartesianPower( [ 'a', 'b', 'c' ], 2 ); + +var v; +while ( true ) { + v = iterator.next(); + if ( v.done ) { + break; + } + console.log( v.value ); +} +``` + +
+ + + + + +
+ +
+ + + + + +
+ +## Examples + + + +```javascript +// Example 1: Generating Cartesian power of an array with n = 2 +var iterator = iterCartesianPower( [ 1, 2, 3 ], 2 ); +// returns + +var v = iterator.next().value; +// returns [ 1, 1 ] + +v = iterator.next().value; +// returns [ 1, 2 ] + +v = iterator.next().value; +// returns [ 1, 3 ] + +v = iterator.next().value; +// returns [ 2, 1 ] + +v = iterator.next().value; +// returns [ 2, 2 ] + +v = iterator.next().value; +// returns [ 2, 3 ] + +v = iterator.next().value; +// returns [ 3, 1 ] + +v = iterator.next().value; +// returns [ 3, 2 ] + +v = iterator.next().value; +// returns [ 3, 3 ] + +var done = iterator.next().done; +// returns true + +// Example 2: Generating Cartesian power of an array with n = 1 +iterator = iterCartesianPower( [ 'a', 'b', 'c' ], 1 ); +// returns + +v = iterator.next().value; +// returns [ 'a' ] + +v = iterator.next().value; +// returns [ 'b' ] + +v = iterator.next().value; +// returns [ 'c' ] + +done = iterator.next().done; +// returns true + +// Example 3: Generating Cartesian power of an array with n = 0 +iterator = iterCartesianPower( [ 'a', 'b', 'c' ], 0 ); +// returns + +v = iterator.next().value; +// returns [] + +done = iterator.next().done; +// returns true +``` + + + + + + + +
+ +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/iter/cartesian-power/benchmark/benchmark.js b/lib/node_modules/@stdlib/iter/cartesian-power/benchmark/benchmark.js new file mode 100644 index 000000000000..07173fe205be --- /dev/null +++ b/lib/node_modules/@stdlib/iter/cartesian-power/benchmark/benchmark.js @@ -0,0 +1,46 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 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 bench = require( '@stdlib/bench' ); +var pkg = require( './../package.json' ).name; +var iterCartesianPower = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var iterator; + var v; + + iterator = iterCartesianPower( [ 'a', 'b', 'c' ], 2 ); + + b.tic(); + v = iterator.next(); + b.toc(); + + if ( v.done ) { + b.fail( 'should not be done' ); + } else { + b.pass( 'single iteration completed' ); + } + b.end(); +} ); diff --git a/lib/node_modules/@stdlib/iter/cartesian-power/docs/repl.txt b/lib/node_modules/@stdlib/iter/cartesian-power/docs/repl.txt new file mode 100644 index 000000000000..f7ce52d2f322 --- /dev/null +++ b/lib/node_modules/@stdlib/iter/cartesian-power/docs/repl.txt @@ -0,0 +1,38 @@ +{{alias}}( x, n ) + Returns an iterator which generates the Cartesian power of an input + array-like object. + + If an environment supports Symbol.iterator, the returned iterator is + iterable. + + Parameters + ---------- + x: collection + Input collection. + + n: NonNegativeInteger + Integer power. + + Returns + ------- + iterator: Object + Iterator. + + iterator.next(): Function + Returns an iterator protocol-compliant object containing the next + iterated value (if one exists) and a boolean flag indicating whether the + iterator is finished. + + iterator.return( [value] ): Function + Finishes an iterator and returns a provided value. + + Examples + -------- + > var it = {{alias}}( [ 'a', 'b', 'c' ], 2 ); + > var v = it.next().value + [ 'a', 'a' ] + > v = it.next().value + [ 'a', 'b' ] + + See Also + -------- diff --git a/lib/node_modules/@stdlib/iter/cartesian-power/docs/types/index.d.ts b/lib/node_modules/@stdlib/iter/cartesian-power/docs/types/index.d.ts new file mode 100644 index 000000000000..a2f392c59483 --- /dev/null +++ b/lib/node_modules/@stdlib/iter/cartesian-power/docs/types/index.d.ts @@ -0,0 +1,63 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2024 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. +*/ + +// TypeScript Version: 4.1 + +/// + +import { Iterator as Iter, IterableIterator } from '@stdlib/types/iter'; +import { ArrayLike } from '@stdlib/types/array'; + +// Define a union type representing both iterable and non-iterable iterators: +type Iterator = Iter | IterableIterator; + +/** +* Returns an iterator which generates the Cartesian power of an input array-like object. +* +* @param x - Input array-like object. +* @param n - Integer power. +* @throws {TypeError} first argument must be an array-like object +* @throws {TypeError} second argument must be a nonnegative integer +* @returns iterator +* +* @example +* var iterCartesianPower = require( '@stdlib/iter/cartesian-power' ); +* +* var iterator = iterCartesianPower( [ 'a', 'b' ], 2 ); +* // returns +* +* var v = iterator.next().value; +* // returns [ 'a', 'a' ] +* +* v = iterator.next().value; +* // returns [ 'a', 'b' ] +* +* v = iterator.next().value; +* // returns [ 'b', 'a' ] +* +* v = iterator.next().value; +* // returns [ 'b', 'b' ] +* +* var bool = iterator.next().done; +* // returns true +*/ +declare function iterCartesianPower( x: ArrayLike | Iterable, n: number ): Iterator; + +// EXPORTS // + +export = iterCartesianPower; diff --git a/lib/node_modules/@stdlib/iter/cartesian-power/docs/types/test.ts b/lib/node_modules/@stdlib/iter/cartesian-power/docs/types/test.ts new file mode 100644 index 000000000000..00b992b0b70b --- /dev/null +++ b/lib/node_modules/@stdlib/iter/cartesian-power/docs/types/test.ts @@ -0,0 +1,57 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2024 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. +*/ + +import iterCartesianPower = require( './index' ); + + +// TESTS // + +// The function returns an iterator... +{ + iterCartesianPower( [ 'a', 'b', 'c' ], 2 ); // $ExpectType Iterator + iterCartesianPower( [ 'a', 'b', 'c' ], 3 ); // $ExpectType Iterator + iterCartesianPower( [ 1, 2, 3 ], 3 ); // $ExpectType Iterator +} + +// The compiler throws an error if the function is provided a first argument which is not an array-like object... +{ + iterCartesianPower( 5, 2 ); // $ExpectError + iterCartesianPower( true, 2 ); // $ExpectError + iterCartesianPower( false, 2 ); // $ExpectError + iterCartesianPower( null, 2 ); // $ExpectError + iterCartesianPower( {}, 2 ); // $ExpectError + iterCartesianPower( ( x: number ): number => x, 2 ); // $ExpectError +} + +// The compiler throws an error if the function is provided a second argument which is not a number... +{ + iterCartesianPower( [ 'a', 'b', 'c' ], '5' ); // $ExpectError + iterCartesianPower( [ 'a', 'b', 'c' ], true ); // $ExpectError + iterCartesianPower( [ 'a', 'b', 'c' ], false ); // $ExpectError + iterCartesianPower( [ 'a', 'b', 'c' ], null ); // $ExpectError + iterCartesianPower( [ 'a', 'b', 'c' ], [] ); // $ExpectError + iterCartesianPower( [ 'a', 'b', 'c' ], {} ); // $ExpectError + iterCartesianPower( [ 'a', 'b', 'c' ], ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided insufficient arguments... +{ + iterCartesianPower(); // $ExpectError + iterCartesianPower( [ 'a', 'b', 'c' ] ); // $ExpectError + iterCartesianPower( [ 'a', 'b', 'c' ], 2, 3 ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/iter/cartesian-power/lib/index.js b/lib/node_modules/@stdlib/iter/cartesian-power/lib/index.js new file mode 100644 index 000000000000..614549666bcc --- /dev/null +++ b/lib/node_modules/@stdlib/iter/cartesian-power/lib/index.js @@ -0,0 +1,55 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 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'; + +/** +* Create an iterator which returns Cartesian powers. +* +* @module @stdlib/iter/cartesian-power +* +* @example +* var iterCartesianPower = require( '@stdlib/iter/cartesian-power' ); +* +* var iterator = iterCartesianPower( [ 'a', 'b' ], 2 ); +* // returns +* +* var v = iterator.next().value; +* // returns [ 'a', 'a' ] +* +* v = iterator.next().value; +* // returns [ 'a', 'b' ] +* +* v = iterator.next().value; +* // returns [ 'b', 'a' ] +* +* v = iterator.next().value; +* // returns [ 'b', 'b' ] +* +* var bool = iterator.next().done; +* // returns true +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/iter/cartesian-power/lib/main.js b/lib/node_modules/@stdlib/iter/cartesian-power/lib/main.js new file mode 100644 index 000000000000..a79e40b54fa4 --- /dev/null +++ b/lib/node_modules/@stdlib/iter/cartesian-power/lib/main.js @@ -0,0 +1,169 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 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 setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var isCollection = require( '@stdlib/assert/is-collection' ); +var isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ).isPrimitive; +var iteratorSymbol = require( '@stdlib/symbol/iterator' ); +var format = require( '@stdlib/string/format' ); +var pow = require( '@stdlib/math/base/special/pow' ); + + +// MAIN // + +/** +* Returns an iterator which generates the Cartesian power of an input collection. +* +* @param {Collection} x - input collection +* @param {NonNegativeInteger} n - integer power +* @throws {TypeError} first argument must be a collection +* @throws {TypeError} second argument must be a nonnegative integer +* @returns {Iterator} iterator +* +* @example +* var iterCartesianPower = require( '@stdlib/iter/cartesian-power' ); +* +* var iterator = iterCartesianPower( [ 'a', 'b' ], 2 ); +* // returns +* +* var v = iterator.next().value; +* // returns [ 'a', 'a' ] +* +* v = iterator.next().value; +* // returns [ 'a', 'b' ] +* +* v = iterator.next().value; +* // returns [ 'b', 'a' ] +* +* v = iterator.next().value; +* // returns [ 'b', 'b' ] +* +* var bool = iterator.next().done; +* // returns true +*/ +function iterCartesianPower( x, n ) { + var iter; + var idx; + var FLG; + var i; + var j; + + // Validate input arguments + if ( !isCollection( x ) ) { + throw new TypeError( format( 'invalid argument. First argument must be a collection. Value: `%s`.', x ) ); + } + if ( !isNonNegativeInteger( n ) ) { + throw new TypeError( format( 'invalid argument. Second argument must be a nonnegative integer. Value: `%s`.', n ) ); + } + + // Initialize index array + idx = []; + for ( j = 0; j < n; j++ ) { + idx.push( 0 ); + } + + // Create an iterator protocol-compliant object + iter = {}; + FLG = false; + i = 0; + setReadOnly( iter, 'next', next ); + setReadOnly( iter, 'return', end ); + + // If an environment supports `Symbol.iterator`, make the iterator iterable + if ( iteratorSymbol ) { + setReadOnly( iter, iteratorSymbol, factory ); + } + return iter; + + /** + * Returns an iterator protocol-compliant object containing the next iterated value. + * + * @private + * @returns {Object} iterator protocol-compliant object + */ + function next() { + var res = []; + var k; + + if ( FLG ) { + return { + 'done': true + }; + } + + for ( k = 0; k < n; k++ ) { + res.push( x[ idx[ k ] ] ); + } + // Update indices + for ( k = n - 1; k >= 0; k-- ) { + idx[ k ] += 1; + if ( idx[ k ] === x.length ) { + idx[ k ] = 0; + } else { + break; + } + } + i += 1; + if ( i >= pow( x.length, n ) ) { + FLG = true; + } + return { + 'value': res, + 'done': false + }; + } + + /** + * Finishes an iterator. + * + * @private + * @param {*} [value] - value to return + * @returns {Object} iterator protocol-compliant object + */ + function end( value ) { + FLG = true; + if ( arguments.length ) { + return { + 'value': value, + 'done': true + }; + } + return { + 'done': true + }; + } + + /** + * Returns a new iterator. + * + * @private + * @returns {Iterator} iterator + */ + function factory() { + return iterCartesianPower( x, n ); + } +} + + +// EXPORTS // + +module.exports = iterCartesianPower; diff --git a/lib/node_modules/@stdlib/iter/cartesian-power/package.json b/lib/node_modules/@stdlib/iter/cartesian-power/package.json new file mode 100644 index 000000000000..57e7d6c7236c --- /dev/null +++ b/lib/node_modules/@stdlib/iter/cartesian-power/package.json @@ -0,0 +1,68 @@ +{ + "name": "@stdlib/iter/cartesian-power", + "version": "0.0.0", + "description": "Create an iterator which generates the Cartesian power of an input array-like object.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdutils", + "stdutil", + "utilities", + "utility", + "utils", + "util", + "cartesian", + "combination", + "permutation", + "iterator", + "iterate", + "iteration", + "iter" + ] + } + \ No newline at end of file diff --git a/lib/node_modules/@stdlib/iter/cartesian-power/test/test.js b/lib/node_modules/@stdlib/iter/cartesian-power/test/test.js new file mode 100644 index 000000000000..60fdbe39b167 --- /dev/null +++ b/lib/node_modules/@stdlib/iter/cartesian-power/test/test.js @@ -0,0 +1,87 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 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 iterCartesianPower = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof iterCartesianPower, 'function', 'main export is a function' ); + t.end(); +} ); + +tape( 'the function returns an iterator which generates all possible combinations of length `n` containing elements from the input array', function test( t ) { + var expected; + var iterator; + var actual; + var v; + + iterator = iterCartesianPower( [ 'a', 'b', 'c' ], 2 ); + expected = [ [ 'a', 'a' ], [ 'a', 'b' ], [ 'a', 'c' ], [ 'b', 'a' ], [ 'b', 'b' ], [ 'b', 'c' ], [ 'c', 'a' ], [ 'c', 'b' ], [ 'c', 'c' ] ]; + actual = []; + + while ( true ) { + v = iterator.next(); + if ( v.done ) { + break; + } + actual.push( v.value ); + } + t.deepEqual( actual, expected, 'returns expected values' ); + t.end(); +} ); + +tape( 'the function returns an iterator which returns an empty array if `n` is 0', function test( t ) { + var iterator; + var expected; + var actual; + + iterator = iterCartesianPower( [ 'a', 'b', 'c' ], 0 ); + expected = []; + actual = iterator.next().value; + t.deepEqual( actual, expected, 'returns expected value' ); + t.end(); +} ); + +tape( 'the function returns an iterator which generates all individual elements of the input array when `n` is 1', function test( t ) { + var expected; + var iterator; + var actual; + var v; + + iterator = iterCartesianPower( [ 'a', 'b', 'c' ], 1 ); + expected = [ [ 'a' ], [ 'b' ], [ 'c' ] ]; + actual = []; + + while ( true ) { + v = iterator.next(); + if ( v.done ) { + break; + } + actual.push( v.value ); + } + t.deepEqual( actual, expected, 'returns expected value' ); + t.end(); +} );