diff --git a/lib/node_modules/@stdlib/iter/cartesian-product/README.md b/lib/node_modules/@stdlib/iter/cartesian-product/README.md new file mode 100644 index 000000000000..118d38adf053 --- /dev/null +++ b/lib/node_modules/@stdlib/iter/cartesian-product/README.md @@ -0,0 +1,139 @@ + + +# iterCartesianProduct + +> Return the [Cartesian product iterator][cartesian-product]. + +
+ +## Usage + +```javascript +var iterCartesianProduct = require('@stdlib/iter/cartesian-product'); +``` + +#### iterCartesianProduct( x1, x2 ) + +Returns the [Cartesian product][cartesian-product]. + +```javascript +var iterCartesianProduct = require('@stdlib/iter/cartesian-product'); +var x1 = [1, 2]; +var x2 = [3, 4]; + +var iter = iterCartesianProduct(x1, x2); + +var v = iter.next().value; +// returns [ 1, 3 ] + +v = iter.next().value; +// returns [ 1, 4 ] + +v = iter.next().value; +// returns [ 2, 3 ] + +// ... +``` + +If provided one or more empty arrays, the function returns an empty array. + +```javascript +var iterCartesianProduct = require('@stdlib/iter/cartesian-product'); +var x1 = [1, 2, 3, 4]; +var x2 = []; + +var iter = iterCartesianProduct(x1, x2); +var v = iter.next().value; +// returns undefined +``` + +The returned [iterator][mdn-iterator-protocol] protocol-compliant object has the following properties: + +- **next**: function which returns an [iterator][mdn-iterator-protocol] protocol-compliant object containing the next iterated value (if one exists) assigned to a `value` property and a `done` property having a `boolean` value indicating whether the [iterator][mdn-iterator-protocol] is finished. +- **return**: function which closes an [iterator][mdn-iterator-protocol] and returns a single (optional) argument in an [iterator][mdn-iterator-protocol] protocol-compliant object. + +
+ + + +
+ +## Notes + +- If an environment supports `Symbol.iterator` **and** a provided [iterator][mdn-iterator-protocol] is iterable, the returned [iterator][mdn-iterator-protocol] is iterable. + +
+ + + +
+ +## Examples + + + +```javascript +var linspace = require('@stdlib/array/linspace'); +var iterCartesianProduct = require('@stdlib/iter/cartesian-product'); + +var x1 = linspace(0, 5, 6); +var x2 = linspace(10, 15, 6); + +var iter = iterCartesianProduct(x1, x2); +var v = iter.next().value; +// returns [ 0, 10 ] +v = iter.next().value; +// returns [ 1, 11 ].... +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/iter/cartesian-product/benchmark/benchmark.js b/lib/node_modules/@stdlib/iter/cartesian-product/benchmark/benchmark.js new file mode 100644 index 000000000000..468e70b27d9b --- /dev/null +++ b/lib/node_modules/@stdlib/iter/cartesian-product/benchmark/benchmark.js @@ -0,0 +1,77 @@ +/** +* @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 zeroTo = require( '@stdlib/array/zero-to' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var isIteratorLike = require( '@stdlib/assert/is-iterator-like' ); +var pkg = require( './../package.json' ).name; +var iterCartesianProduct = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var x; + var i; + var v; + + x = zeroTo( 100 ); + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = iterCartesianProduct( x, x ); + if ( typeof v !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( !isIteratorLike( v ) ) { + b.fail( 'should return an iterator protocol-compliant object' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::iterator', function benchmark( b ) { + var x; + var i; + var j; + var v; + + x = zeroTo( 100 ); + + v = iterCartesianProduct( x, x ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + j = v.next().value; + if ( isnan( j ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( !isIteratorLike( v ) ) { + b.fail( 'should return an iterator protocol-compliant object' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/iter/cartesian-product/docs/repl.txt b/lib/node_modules/@stdlib/iter/cartesian-product/docs/repl.txt new file mode 100644 index 000000000000..67d39e1fae9c --- /dev/null +++ b/lib/node_modules/@stdlib/iter/cartesian-product/docs/repl.txt @@ -0,0 +1,39 @@ + +{{alias}}( x1, x2 ) + Returns an iterator which returns the cartesian product of the input + arrays. + + If an environment supports Symbol.iterator, the returned iterator is + iterable. + + Parameters + ---------- + x1: Collection + First input array. + + x2: Collection + Second input array. + + 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}}( [0, 1], [2, 3]); + > var v = it.next().value + [0, 2] + > v = it.next().value + [0, 3] + + See Also + -------- diff --git a/lib/node_modules/@stdlib/iter/cartesian-product/docs/types/index.d.ts b/lib/node_modules/@stdlib/iter/cartesian-product/docs/types/index.d.ts new file mode 100644 index 000000000000..69a315174ed3 --- /dev/null +++ b/lib/node_modules/@stdlib/iter/cartesian-product/docs/types/index.d.ts @@ -0,0 +1,60 @@ +/* +* @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. +*/ + +/* eslint-disable @typescript-eslint/no-explicit-any */ + +// TypeScript Version: 4.1 + +/// + +import { Iterator as Iter, IterableIterator } from '@stdlib/types/iter'; + +// Define a union type representing both iterable and non-iterable iterators: +type Iterator = Iter | IterableIterator; + +/** + * Returns an iterator which generates all combinations of elements from the input arrays. + * + * ## Notes + * + * - If an environment supports `Symbol.iterator`, the returned iterator is iterable. + * + * @param x1 - first input array + * @param x2 - second input array + * @returns iterator + * + * @example + * var iter = iterCartesianProduct( [ 1, 2 ], [ 3, 4 ] ); + * + * var v = iter.next().value; + * // returns [ 1, 3 ] + * + * v = iter.next().value; + * // returns [ 1, 4 ] + * + * v = iter.next().value; + * // returns [ 2, 3 ] + * + * // ... + */ +declare function iterCartesianProduct( x1: Array, x2: Array ): Iterator; + + +// EXPORTS // + +export = iterCartesianProduct; diff --git a/lib/node_modules/@stdlib/iter/cartesian-product/docs/types/test.ts b/lib/node_modules/@stdlib/iter/cartesian-product/docs/types/test.ts new file mode 100644 index 000000000000..2ef8eab92fc4 --- /dev/null +++ b/lib/node_modules/@stdlib/iter/cartesian-product/docs/types/test.ts @@ -0,0 +1,56 @@ +/* +* @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 iterCartesianProduct = require( './index' ); + + +// TESTS // + +// The function returns an iterator... +{ + iterCartesianProduct( [ 1, 2, 3 ], [ 4, 5, 6 ] ); // $ExpectType Iterator + iterCartesianProduct( 'a', 'c' ); // $ExpectError + iterCartesianProduct( undefined, undefined ); // $ExpectError + iterCartesianProduct( null, null ); // $ExpectError + iterCartesianProduct( true, true ); // $ExpectError +} + +// The compiler throws an error if the function is provided a first argument which is not array-like... +{ + iterCartesianProduct( 2, [ 4, 5, 6 ] ); // $ExpectError + iterCartesianProduct( false, [ 4, 5, 6 ] ); // $ExpectError + iterCartesianProduct( true, [ 4, 5, 6 ] ); // $ExpectError + iterCartesianProduct( {}, [ 4, 5, 6 ] ); // $ExpectError + iterCartesianProduct( ( x: number ): number => x, [ 4, 5, 6 ] ); // $ExpectError +} + +// The compiler throws an error if the function is provided a last argument which is not array-like... +{ + iterCartesianProduct( [ 1, 2, 3 ], 2 ); // $ExpectError + iterCartesianProduct( [ 1, 2, 3 ], false ); // $ExpectError + iterCartesianProduct( [ 1, 2, 3 ], true ); // $ExpectError + iterCartesianProduct( [ 1, 2, 3 ], {} ); // $ExpectError + iterCartesianProduct( [ 1, 2, 3 ], ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + iterCartesianProduct(); // $ExpectError + iterCartesianProduct( [ 1, 2, 3 ] ); // $ExpectError + iterCartesianProduct( [ 1, 2, 3 ], [ 4, 5, 6 ], 2 ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/iter/cartesian-product/examples/index.js b/lib/node_modules/@stdlib/iter/cartesian-product/examples/index.js new file mode 100644 index 000000000000..7147dfc2160a --- /dev/null +++ b/lib/node_modules/@stdlib/iter/cartesian-product/examples/index.js @@ -0,0 +1,37 @@ +/** +* @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'; + +var linspace = require('@stdlib/array/linspace'); +var iterCartesianProduct = require('./../lib'); + +var x1 = linspace(0, 5, 6); +var x2 = linspace(10, 15, 6); + +var iter = iterCartesianProduct(x1, x2); + +// Perform manual iteration... +var v; +while (true) { + v = iter.next(); + if (v.done) { + break; + } + console.log(v.value); +} diff --git a/lib/node_modules/@stdlib/iter/cartesian-product/lib/index.js b/lib/node_modules/@stdlib/iter/cartesian-product/lib/index.js new file mode 100644 index 000000000000..1e2a4ceb48ee --- /dev/null +++ b/lib/node_modules/@stdlib/iter/cartesian-product/lib/index.js @@ -0,0 +1,51 @@ +/** +* @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 generates a Cartesian product of input arrays. +* +* @module @stdlib/iter/cartesian-product +* +* @example +* var iterCartesianProduct = require( '@stdlib/iter/cartesian-product' ); +* +* var iter = iterCartesianProduct([1, 2], [3, 4]); +* +* var v = iter.next().value; +* // returns [1,3] +* +* v = iter.next().value; +* // returns [1,4] +* +* v = iter.next().value; +* // returns [2,3] +* +* v = iter.next().value; +* // returns [2,4] +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/iter/cartesian-product/lib/main.js b/lib/node_modules/@stdlib/iter/cartesian-product/lib/main.js new file mode 100644 index 000000000000..5460d70065fe --- /dev/null +++ b/lib/node_modules/@stdlib/iter/cartesian-product/lib/main.js @@ -0,0 +1,137 @@ +/** +* @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 base = require('@stdlib/array/base/cartesian-product'); +var iteratorSymbol = require('@stdlib/symbol/iterator'); +var format = require('@stdlib/string/format'); + + +// MAIN // + +/** +* Returns an Iterator of Cartesian product of arrays. +* +* @param {Collection} x1 - first input array +* @param {Collection} x2 - second input array +* @throws {TypeError} first argument must be a collection +* @throws {TypeError} second argument must be a collection +* @returns {Iterator} iterator +* +* @example +* var iter = iterCartesianProduct([1, 2], [3, 4]); +* +* var v = iter.next().value; +* // returns [1,3] +* +* v = iter.next().value; +* // returns [1,4] +* +* v = iter.next().value; +* // returns [2,3] +* +* v = iter.next().value; +* // returns [2,4] +*/ +function iterCartesianProduct( x1, x2 ) { + var value; + var iter; + var prod; + var i; + + if ( !isCollection( x1 ) ) { + throw new TypeError( format( 'invalid argument. First argument must be a collection. Value: `' + x1 + '`.' ) ); + } + if ( !isCollection( x2 ) ) { + throw new TypeError( format( 'invalid argument. Second argument must be a collection. Value: `' + x2 +'`.' ) ); + } + + prod = base( x1, x2 ); + + // Create an iterator protocol-compliant object: + iter = {}; + 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() { + if ( i >= prod.length ) { + return { + 'done': true + }; + } + value = prod[i]; + i += 1; + return { + 'value': value, + 'done': false + }; + } + + /** + * Finishes an iterator. + * + * @private + * @param {*} [value] - value to return + * @returns {Object} iterator protocol-compliant object + */ + function end( value ) { + i = prod.length; + if ( arguments.length ) { + return { + 'value': value, + 'done': true + }; + } + return { + 'done': true + }; + } + + /** + * Returns a new iterator. + * + * @private + * @returns {Iterator} iterator + */ + function factory() { + return iterCartesianProduct( x1, x2 ); + } +} + + +// EXPORTS // + +module.exports = iterCartesianProduct; diff --git a/lib/node_modules/@stdlib/iter/cartesian-product/package.json b/lib/node_modules/@stdlib/iter/cartesian-product/package.json new file mode 100644 index 000000000000..27207e8beace --- /dev/null +++ b/lib/node_modules/@stdlib/iter/cartesian-product/package.json @@ -0,0 +1,66 @@ +{ + "name": "@stdlib/iter/cartesian-product", + "version": "0.0.0", + "description": "Return the Cartesian product iterator.", + "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", + "utilities", + "utils", + "generic", + "array", + "cartesian", + "product", + "iterator", + "sets", + "math", + "mathematics", + "ordered" + ] + } + \ No newline at end of file diff --git a/lib/node_modules/@stdlib/iter/cartesian-product/test/test.js b/lib/node_modules/@stdlib/iter/cartesian-product/test/test.js new file mode 100644 index 000000000000..1e5e49b4131e --- /dev/null +++ b/lib/node_modules/@stdlib/iter/cartesian-product/test/test.js @@ -0,0 +1,245 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2019 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 toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +var iterCartesianProduct = require( './../lib' ); + + +// TESTS // + +tape('main export is a function', function test(t) { + t.ok(true, __filename); + t.strictEqual(typeof iterCartesianProduct, 'function', 'main export is a function'); + t.end(); +}); + +tape('the function throws an error if provided a first argument which is not a collection', function test(t) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + {}, + function noop() {} + ]; + for (i = 0; i < values.length; i++) { + t.throws(badValue(values[i]), TypeError, 'throws an error when provided ' + values[i]); + } + t.end(); + + function badValue(value) { + return function badValue() { + iterCartesianProduct(value, [1, 2]); + }; + } +}); + +tape('the function throws an error if provided a second argument which is not a collection', function test(t) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + {}, + function noop() {} + ]; + for (i = 0; i < values.length; i++) { + t.throws(badValue(values[i]), TypeError, 'throws an error when provided ' + values[i]); + } + t.end(); + + function badValue(value) { + return function badValue() { + iterCartesianProduct([1, 2], value); + }; + } +}); + +tape('the function returns the Cartesian product iterator (indexed)', function test(t) { + var iterator; + var expected; + var actual; + var i; + + iterator = iterCartesianProduct([1, 2], [3, 4]); + + expected = [ + { + 'value': [1, 3], + 'done': false + }, + { + 'value': [1, 4], + 'done': false + }, + { + 'value': [2, 3], + 'done': false + }, + { + 'value': [2, 4], + 'done': false + }, + { + 'done': true + } + ]; + + actual = []; + + for (i = 0; i < expected.length; i++) { + actual.push(iterator.next()); + } + + t.deepEqual(actual, expected, 'returns expected value'); + t.end(); +}); + +tape('the function returns a empty iterator if either of the input iterators are empty (indexed)', function test(t) { + var iterator; + var expected; + var actual; + var i; + + iterator = iterCartesianProduct([], [3, 4]); + expected = [{ + 'done': true + }]; + actual = []; + for (i = 0; i < expected.length; i++) { + actual.push(iterator.next()); + } + t.deepEqual(actual, expected, 'returns expected value'); + + iterator = iterCartesianProduct([1, 2], []); + expected = [{ + 'done': true + }]; + actual = []; + for (i = 0; i < expected.length; i++) { + actual.push(iterator.next()); + } + t.deepEqual(actual, expected, 'returns expected value'); + + iterator = iterCartesianProduct([], []); + expected = [{ + 'done': true + }]; + actual = []; + for (i = 0; i < expected.length; i++) { + actual.push(iterator.next()); + } + t.deepEqual(actual, expected, 'returns expected value'); + t.end(); +}); + +tape('the function returns the Cartesian product iterator (accessors)', function test(t) { + var expected; + var actual; + var it; + var i; + + it = iterCartesianProduct(toAccessorArray([1, 2]), toAccessorArray([3, 4])); + + expected = [ + { + 'value': [1, 3], + 'done': false + }, + { + 'value': [1, 4], + 'done': false + }, + { + 'value': [2, 3], + 'done': false + }, + { + 'value': [2, 4], + 'done': false + }, + { + 'done': true + } + ]; + + actual = []; + + for (i = 0; i < expected.length; i++) { + actual.push(it.next()); + } + + t.deepEqual(actual, expected, 'returns expected value'); + t.end(); +}); + +tape('the function returns a empty iterator if either of the input iterators are empty (accessors)', function test(t) { + var expected; + var actual; + var it; + var i; + + it = iterCartesianProduct(toAccessorArray([]), toAccessorArray([3, 4])); + expected = [{ + 'done': true + }]; + actual = []; + + for (i = 0; i < expected.length; i++) { + actual.push(it.next()); + } + t.deepEqual(actual, expected, 'returns expected value'); + + it = iterCartesianProduct(toAccessorArray([1, 2]), toAccessorArray([])); + expected = [{ + 'done': true + }]; + actual = []; + for (i = 0; i < expected.length; i++) { + actual.push(it.next()); + } + t.deepEqual(actual, expected, 'returns expected value'); + + it = iterCartesianProduct(toAccessorArray([]), toAccessorArray([])); + expected = [{ + 'done': true + }]; + actual = []; + for (i = 0; i < expected.length; i++) { + actual.push(it.next()); + } + t.deepEqual(actual, expected, 'returns expected value'); + t.end(); +});